Zvini
17:26:54

« API Documentation » PHP Example

Below is a PHP code that calls an example API method:
// prepare parameters
$api_base = 'https://zvini.com/api-call/';
$api_key = 'DPFvTPfRvNPLfVvR4...';
$method = 'someNamespace/someMethod';
$params = [
    'api_key' => $api_key,
    'param1' => 'value1',
    'param2' => 'value2',
    // ...
];

// send request
$ch = curl_init($api_base.$method);
curl_setopt_array($ch, [
    CURLOPT_POSTFIELDS => http_build_query($params),
    CURLOPT_RETURNTRANSFER => true,
]);
$response = curl_exec($ch);

// check for errors
if ($response === false) {
    die('ERROR: '.curl_error($ch));
}
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
    die('ERROR: '.$response);
}

// decode json
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
if ($contentType === 'application/json') {
    $response = json_decode($response);
}

// do something with the response