Hello World Example

Let’s see how to list, for example, users. Save the following code as ‘vcc-db-api.php’ onto your web server, then open it, change the credential information as required (see: Authentication and Security, and finally save it.

<?php
error_reporting(0);
 
// settings
$customer = 'CUSTOMER';
$password = 'PASSWORD';
 
// build Database API URL
$url = sprintf('https://%s:%s@%s.asp.virtual-call-center.eu', $customer, $password, $customer);
 
// build resource
$resource = $url . '/v2/users';
 
// send HTTP GET request to API and obtain the result
$http_response = file_get_contents($resource);

if ($http_response === false) {
 
    // there is an error
    $last_error = error_get_last();
    echo "Connection error:n" . $last_error['message'] . "nn";
    exit(1);
}
 
// $http_response_header global var contains HTTP response code
list(, $http_response_code) = explode(' ', $http_response_header[0]);
 
// check if HTTP response is OK
if ($http_response_code != 200) {
 
    // there is an error
    echo 'Database API error code: ' . $http_response_code . "nn";
    exit(1);
}
 
echo "Output in JSON format:n" . $http_response . "nn";

// convert json structure into php array
$vcc_users = json_decode($http_response, true);

// print users array
echo "Output as PHP array:n";
var_export($vcc_users);
echo "nn";

Type the URL into any browser (e.g.: http://your.url/vcc-db-api.php), press Enter, and check the output on your screen, which should look similar to the following:

{
    "response": [
        {
            "status": "active",
            "name": "Admin",
            "username": "admin",
            "extension": "4",
            "userid": 4,
            "teams_name": "default_team",
            "group_name": "admin"
        },
        {
            "status": "active",
            "name": "Supervisor",
            "username": "supervisor",
            "extension": "5",
            "userid": 5,
            "teams_name": "default_team",
            "group_name": "supervisor"
        },
        {
            "status": "active",
            "name": "mozmill operator",
            "username": "mozmill_operator",
            "extension": "104",
            "userid": 9,
            "teams_name": "default_team",
            "group_name": "operator"
        }
    ],
    "errors": []
}

Using http://jsonlint.com/ you can convert JSON texts into a readable format, and also validate them. The example code should hopefully be easy to understand, but one section may need some explanation. When you call file_get_contents() function it returns with the HTTP response body and also sets the $http_response_header global variable. It should be checked whether the HTTP response code is 200 or not. If it is not, please check HTTP Response Codes for possible errors.

Note: Do not use this code in production, as the file_get_contents() function is not able to send HTTP POST, PUT and DELETE requests. The above code is only for demonstration.


There are no comments yet.

Comment

Subscribe to our newsletter
or Contact us!