REST API Specification – ApiRestHelper class
This is a PHP helper class that is used in all PHP samples in API specification, so you don’t have to repeat the code in all samples.
Function ApiRestHelper::createApiCall($url, $method, $headers, $data = array()) accepts the following parameters:
$url is api call url.
$method is a request method. It can be ‘PUT’, ‘POST’, ‘GET’ or ‘DELETE’.
$headers is an array of HTTP headers to be sent with the request.
$data is an array of data to be sent with the request.
class ApiRestHelper
{
public static function createApiCall($url, $method, $headers, $data = array())
{
if ($method == 'PUT')
{
$headers[] = 'X-HTTP-Method-Override: PUT';
}
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
switch($method)
{
case 'GET':
break;
case 'POST':
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($data));
break;
case 'PUT':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($data));
break;
case 'DELETE':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
}
$response = curl_exec($handle);
return $response;
}
}