Advanced Example

Reading Time: 3 minutes

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.
Subscribe to our newsletter
or Contact us!
Please note that by subscribing to our newsletter, you agree to receive regular email messages from VCC Live® about service related news and updates. By subscribing to our newsletter, you also agree that VCC Live® will use your data in accordance with the applicable Privacy Policy until you unsubscribe from the newsletter.

Privacy Settings Center

Necessary

Necessary cookies are essential in order to navigate around a website and use its basic features. You must accept these cookies as the website cannot function properly without them.

__cfduid,debug,PHPSESSID

Marketing

Marketing cookies are used to track visitors across websites and deliver advertisements that are more relevant and engaging to you.

__atuvc,__atuvs,_at.cww,_hjIncludedInSample,_hjRecordingEnabled,ads/user-lists/#,at-lojson-cache-#,at-rand,bt2,di2,_fbp,IDE,lidc,loc,NID,ouid,r/collect,rc::a,rc::b,s,test_cookie,uid,uvc,vc,xtc

Statistics

Statistic cookies collect data to help us understand how visitors use our websites and are used to improve the user experience. These cookies don’t collect personal information and cannot identify visitors.

_ceg.s,_ceg.u,_ga,_gat,_gid,_gat_UA-1696494-11

Other

Other cookies are those cookies that do not fall into the categories above. These cookies are in the process of being classified.

loglevel,socketCluster.authToken,u,webchatClientId,webchatOpen