Let's use the RESTClient
class to consume some services. Let us use the library example service from previous chapters first. Here is the PHP client code for getting the books.
<?php require "RESTClient.php"; $client = new RESTClient(); $result = $client->get("http://localhost/rest/09/library.php/book", array()); printf("Response = %s <br>", htmlspecialchars($result)); ?>
First, we indicate that we require the source file with RESTClient
PHP
class. We use a require statement to do this. Obviously, we assume that the PHP class is defined in a file with the name RESTClient
.php
.
require "RESTClient.php";
Next we create an instance of the RESTClient
class.
$client = new RESTClient();
And we send a GET
request to the service URL.
$result = $client->get("http://localhost/rest/09/library.php/book", array());
Note that the request parameters array is empty because we do not have any parameters to be sent to the service.
Finally we print out the result.
printf("Response = %s <br>", htmlspecialchars($result));
Note how the use of the RESTClient
class has simplified our PHP code to consume the service a great deal.