Using PHP to access the REST API

In this recipe, we will quickly look at how you can build an easy webpage using PHP to access the Orchestrator REST API.

Getting ready

We will make our life a bit easier and use a bit of help with PHP and REST. We will use Nate Good's HTTPFUL, which you can find at http://phphttpclient.com/ .

You need to download the following file, httpful.phar, from his website and place it in the same directory as your script.

You also need a Webserver that uses PHP and has cURL activated. I used LAMP stack from Turnkey https://www.turnkeylinux.org/lampstack . See the There's more section for a fast how-to.

How to do it...

This is a quick intro only:

  1. Edit a file such as callWorkflow.php
  2. Enter the following code:
          <?php 
          include('./httpful.phar'); 
     
          $usr = '[email protected]'; 
          $pwd = 'What4Ever'; 
          $vroServer = '192.168.220.12:8281'; 
          // Example Workflow "07.01 Access via PowerShell" 
          $wfid = '48e10dcf-998c-4db7-b93d-144678e15368'; 
     
          // URL for the request 
          $uri = "https://{$vroServer}/vco/api/workflows/{$wfid}/executions"; 
          $response = HttpfulRequest::post($uri) // use post 
             ->sendsJson()           //conent-typ:applicalion/json 
              ->basicAuth($usr,$pwd)  //use basic Authentication 
              ->body("{}")            //empty jsonboady 
              ->withoutStrictSsl()    //ignore SSL certs 
              ->send();               //send it off                  
     
          //get the Associative array out of the response headers 
          $location=$response->headers; 
          //get the location for the workflow execution 
          echo $location[location]; 
          ?> 
    
  3. Save the workflow and access the webpage using http://[ip]/ callWorkflow.php.
  4. You should see a response location.

How it works...

Using PHP mostly employs the same method as using PowerShell or the Swagger UI. You post a request, you get a response, you parse the response.

Using this as a stepping stone, you can now develop your own web services for Orchestrator. The full HTTPFUL document can be found here: http://phphttpclient.com/docs/class-Httpful.Request.html

Please have a look at the recipe Turning strings into objects in Chapter 6, Advanced Programming, to understand how to create an object out of a string.

There's more...

Here is how to get the Turnkey LAMP ready to rumble in some short steps:

  1. Download and deploy the ova image from https://www.turnkeylinux.org/lampstack .
  2. Open a console. When asked, define passwords and skip the rest.
  3. Connect and log in as root to the web console https://[ip] :12320.
  4. Run the following commands:
          apt-get update
          apt-get install curl libcurl3php5-curl
          mv /etc/php5/mods-available/xcache.ini /etc/php5/mods-
              available/xcache.ini.OLD
    
  5. Reboot the appliance.
  6. You can now upload your .php script to /var/www and call it directly with http://[ip]/[scipt.php]. There is also a file manager in the web console, https://[ip]:12321/filemin/.

See also

  • Running Orchestrator workflows using PowerShell in this chapter
  • Working with REST in Chapter 9, Essential Plugins
  • Accessing the Control Center API via REST in this chapter
  • Turning strings into objects in Chapter 6, Advanced Programming
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset