Here is a simple RESTClient
class that you can use to consume services.
The name of the PHP class is RESTClient
. This class does not use any other framework and is based solely on basic PHP constructs. Hence you can easily use this class on its own without having to install any other framework. This would be useful if you want to just consume services using some lightweight PHP code.
We use a private variable in the PHP code to track whether to use CURL or not based on the availability of CURL in the PHP system installed.
private $with_curl;
While creating an instance of the client, the constructor of the class either chooses to use CURL if the CURL functions are available or else it would use fopen
functions for communicating with the services.
public function __construct() { if (function_exists("curl_init")) { $this->with_curl = TRUE; } else { $this->with_curl = FALSE; } }
The RESTClient
class has four member functions get
, post
, put,
and delete
to work with HTTP verbs GET
, POST
, PUT
and DELETE
respectively. While using these methods of the class we must pay attention to the parameters that each method takes.