You can use the REST API in an application written in PHP code to access Oracle B2C Service from a web page. Let's look at an example where you can retrieve the details of a Contact object using PHP. Using a web page, you enter a contact's First Name and Last Name. Then, you click the Get button to retrieve the ID of that contact.
Note:
Do not use the code shown in this example in a production environment.
To retrieve the details of a Contact object using PHP:
- Create a web page using HTML and PHP by using a text editor such as Notepad++.
- Create a form with two input boxes named 'fname' and 'lname'. For example:
<form name="form1" ACTION="<?php echo $PHP_SELF;?>">
<p>
ENTER CONTACT FIRST NAME <input type="text" name="fname"/>
<br>
ENTER CONTACT LAST NAME <input type="text" name="lname"/>
<br>
</form>
- 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'];
$url="https://Admin1:Admin1234cd158d124.qb.lan/services/rest/connect/v1.3/contacts?q=name.first"".$fn.""%20AND%20name.last"".$ln."";
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.
- Load the cURL library to send HTTP requests in PHP. For example:
if (!extension_loaded('curl'))
{
load_curl();
}
$ch = curl_init($url);
- 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");
- Set up the following to avoid security issues for this example:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- Add the code to make the call to the server and load the returned value in a variable named 'response'. For example:
try
{
$response = curl_exec($ch);
}
- Catch any errors in the call, such as network errors. For example:
catch (Exception $e)
{
echo 'Error message: ' .$e->getMessage();
}
- 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);
curl_close($ch);
- 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);
- 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;
}
}
- Print the entire response. For example:
<div id="response">
Response:
<?php
if ($info->items != null)
print $response;
?>
- 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:
<?php
print $info->items[0]->id;
?>
- Save and close the HTML file.
- Open the HTML page in a web browser.
- Enter the values for First Name and Last Name and click Get.
The full response and the parsed ID are displayed.