Apart from a simple GET query, you
can also use DELETE for deletion, PATCH for updates, POST for creating
new objects, and OPTIONS to view the REST verbs that can be used for
a particular resource in the REST API .
Note:
Do not
use the code shown in this example in a production environment.
Let's look at an example which shows
a simplified PHP application to do a PATCH operation:
- Create a
.php file using HTML and PHP by using a text editor such as Notepad++.
- In the .php
file, inside the <body> tag and just before the <php> tag add
a message. This message will indicate that the page has been loaded.
For example:
<body>
Patching contact 6
<?PHP>
if(!extension_loaded('curl'))
Loading the page displays the message and runs
the PHP REST call.
Note:
Do not use the code shown in this example in
a production environment. Some elements, such as resource record numbers
and login credentials are hard-coded, because it is a demo. For example:
$url = "https://Admin1:Admin1234@nancy165.gb.lan/services/rest/connect/v1.3/contacts/6"
- Set up the
data that you want to update, using JSON. In this case, you are going
to update the last name of contact number 6 to 'Ryder'.
$json_data = '("name": ("last": "Ryder"))';
- Set up the
CURL options.
- Set up the URL:
curl_setopt($ch, CURLOPT_URL, $url);
- Set up the access. In this demo, the code
is set to ignore the certificate, which you should not do in a production
environment:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- Set the verb to PATCH for the update:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
- Set the JSON-encoded data:
curl_setopt($ch, CURLOPT_POSTFIELDS, $jason_data);
- Add the commands
to run the application and catch errors, if any:
try {
curl_exec($ch);
}
catch(Exception $e) {
echo "Catch error message: " .$e->getMessage();
}
if (curl_errno($ch)) {
echo "Error accessing site: errno: ".curl_error($ch);
}
curl_close($ch);
- Save and
close the PHP file.
- Go to the
Agent Desktop and verify the current last name of contact number 6.
In this case, the current last name is 'Wright'.
- Open the
PHP file in a web browser.
This runs the PHP application.
- Refresh your
Agent Desktop and verify that the last name of contact number 6 has
been updated to 'Ryder'.