B2C Service | Use the B2C Service REST API in PHP


[music] Hello, my name’s Nancy. In this tutorial I’ll show you an example of using the REST API in PHP code to access Oracle Service Cloud from a webpage. Here I have a little demo application: a form on a page which is hosted here on my local machine but of course it could be hosted on any server anywhere. You can see that what I’m doing here is having the user enter the first and last name of a contact and then returning the ID for that contact. You could then use the ID to retrieve other data about the contact or to delete it, for example. We’re going to look the code behind this and let me remind you that this is just a demo. It’s deliberately simplified so there are elements missing and the approach to security is pretty casual. In other words, this is not code that you should copy and use in production. With that said let me show you that first here I have HTML to create the form and two text input boxes named “fname” and “lname”. And then we get into PHP. I’ll get the values of the first and last name from the form and use them to create this URL that uses a query to locate the contact by first and last name. There’s a little possible “gotcha” here that I want to point out. See these ‘%20’s here? Those are symbols for a space. Spaces are required around the logical operator “AND” if you use it in a query in the REST API but spaces aren’t allowed in an HTTP URL so I’m using this percent encoding where percent 20 represents a space to get around that. Now I’ll load the CURL library which is one way to send http requests in PHP. I’ll do a little setup here like telling CURL to return the response as a string without the response header and then here I’ll set up the appropriate HTTP Verb which is “GET” because I’m just fetching data. This next line sidesteps some of the normal security and remember I’m taking some shortcuts here because this is just a demo application. And then here is where I make the call to the server. And the return value will be put here into a variable named “response”. I’ll catch any errors in the call like network errors for example, and then I’ll also print any CURL error that pops up, like being unable to resolve the server, before I close the CURL connection. The response from a REST API call The response from a REST API call is encoded in JSON so I’m using the “json_decode” function to parse it out and store it in a variable named “info”. Now I’ll check to see if the response included an HTML status, which it will if there was an error, so I’ll print out what that was. Here I’m just printing out the entire response so I can see it. And then finally here I’ll use that info object to get the data I want, which is the ID in the first item, and in this case the only item, that’s in the returned response, which is an array of items. Now back to the browser and let’s run this. Here’s a first and last name and when I click “Get” you can see the full response here Here’s a first and last name and when I click “Get” you can see the full response here and then the parsed out ID here. This is just to show you that you could extract any piece of data you want from the response. Thanks for watching. [music]

Leave a Reply

Your email address will not be published. Required fields are marked *