Advanced Example

The following example uses PHP cURL module, and thus it can send all four supported HTTP request methods (GET, PUT, POST and DELETE). The example consists of a library and the example code.

<?php
useVCCAPIv2RestClient;
require_once ('../RestClient.php');
 
$customer = 'bdevel';
$password = 'PASSWORD';
$apiClient = new RestClient($customer, $password);
 
if (!$apiClient->get('/v2/projects')) {
    echo "Error(1):n";
    echo 'Status code: ' . $apiClient->response_code . "n";
    echo $apiClient->error . "nn";
    exit(1);
}
 
$projects = $apiClient->result['response'];
var_export($projects);
$selected_projectid = $projects[0]['projectid'];
 
if (!$apiClient->get('/v2/projects/' . $selected_projectid)) {
    echo 'Error(2): ' . $apiClient->response_code . "n";
    exit(2);
}
 
$project = $apiClient->result;
var_export($project);
<?php
namespaceVCCAPIv2;
useInvalidArgumentException;
/**
 * Rest API client
 */
class RestClient
 
{
    const DOMAIN_PATTERN = 'https://%s.asp.virtual-call-center.eu';
    public $http_info;
 
    public $result;
 
    public $response_code;
 
    public $connect_timeout = 3;
 
    public $exec_timeout = 60;
 
    public $error;
 
    public
 
    function __construct($customer, $password, $options = array())
    {
        $this->url = sprintf(self::DOMAIN_PATTERN, $customer);
        $this->username = $customer;
        $this->password = $password;
        $this->options = $options;
    }
 
    public
 
    function get($resource)
    {
        return $this->call('GET', $resource);
    }
 
    public
 
    function post($resource, $data)
    {
        return $this->call('POST', $resource, $data);
    }
 
    public
 
    function put($resource, $data)
    {
        return $this->call('PUT', $resource, $data);
    }
 
    public
 
    function delete($resource)
    {
        return $this->call('DELETE', $resource);
    }
 
    protected
    function call($method, $resource, $data = null)
    {
        unset($this->http_info);
        unset($this->result);
        unset($this->response_code);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->url . $resource);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if (isset($this->username) and isset($this->password)) {
            curl_setopt($ch, CURLOPT_USERPWD, $this->username . ':' . $this->password);
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        }
 
        if (isset($this->options['accept_invalid_ssl']) and $this->options['accept_invalid_ssl']) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
 
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connect_timeout);
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->exec_timeout);
        if ($data) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-type: application/json'
            ));
            $data = json_encode($data);
        }
 
        switch ($method) {
        case 'GET':
            break;
 
        case 'PUT':
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'X-HTTP-Method-Override: PUT'
            ));
            break;
 
        case 'POST':
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'X-HTTP-Method-Override: POST'
            ));
            break;
 
        case 'DELETE':
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'X-HTTP-Method-Override: DELETE'
            ));
            break;
 
        case 'OPTIONS':
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'OPTIONS');
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'X-HTTP-Method-Override: OPTIONS'
            ));
            break;
 
        default:
            throw new InvalidArgumentException('HTTP method invalid: ' . $method);
        }
 
        set_time_limit($this->exec_timeout + 1);
        $json_result = curl_exec($ch);
        $this->result = json_decode($json_result, true);
        $this->http_info = curl_getinfo($ch);
        $this->error = curl_error($ch);
        $this->response_code = $this->http_info['http_code'];
        curl_close($ch);
        if ($this->response_code == '200') {
            return true;
        }
 
        return false;
    }
}

There are no comments yet.

Comment

Subscribe to our newsletter
or Contact us!