Video: Using the REST API in PHP

You can use the REST API in an application written in PHP code to access Oracle Cloud from a web page. In this example, we retrieve the details of a Contact object using PHP. Using a web page, we enter a contact's First Name and Last Name. Clicking the Get button retrieves the ID of that contact.


Do not use the code shown in this example in a production environment.
To retrieve the details of a Contact object using PHP:
  1. Create a web page using HTML and PHP by using a text editor such as Notepad++.
  2. Create a form with two input boxes named 'fname' and 'lname'. For example:
    <form name="form1" ACTION="<?php echo $PHP_SELF;?>">
    ENTER CONTACT FIRST NAME <input type="text" name="fname"/>
    ENTER CONTACT LAST NAME <input type="text" name="lname"/>
  3. In the PHP code, get the values of the first and last name from the form and use them to create the URL that uses a query to locate the contact by first and last name. For example:
    $ln = $_GET['lname'];
    $fn = $_GET['fname'];

    The %20 used here are symbols for space. Spaces are required around the logical operator AND if you use it in a query in REST API, but spaces aren't allowed in an HTTP URL.

  4. Load the cURL library to send HTTP requests in PHP. For example:
    if (!extension_loaded('curl'))
    $ch  = curl_init($url);
  5. Setup cURL to return the response as a string without the response header and setup the HTTP verb as GET to fetch data. For example:
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_CUSTOMEREQUEST, "GET");
  6. Set up the following to avoid security issues for this example:
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  7. Add the code to make the call to the server and load the returned value in a variable named 'response'. For example:
      $response = curl_exec($ch);
  8. Catch any errors in the call, such as network errors. For example:
    catch (Exception $e)
      echo 'Error message: ' .$e->getMessage();
  9. Print any cURL errors that might occur before closing the cURL connection. For example:
    if (curl_errorno($ch));
        print "Curl error number: ".curl_error($ch);
  10. The response from a REST API call is encoded in JSON, so use the json_decode function to parse it and store it in a variable named 'info'. For example:
    $info = json_decode($response);
  11. In case of error, print the response included in the HTML status. For example:
    if ($info->status)
      if ($info->status != 200)
         print "<p>Error: ".$info->status; 
  12. Print the entire response. For example:
    <div id="response">
    if ($info->items != null)
        print $response;
  13. Use the info object to get the ID. The info object is an array of items and the ID is available in the first item of the array .For example:
    ID from parsed response:
        print $info->items[0]->id;
  14. Save and close the HTML file.
  15. Open the HTML page in a web browser.
  16. Enter the values for First Name and Last Name and click Get.

    The full response and the parsed ID are displayed.