Zvini
17:31:51

« Cross-site API Documentation » PHP Example

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

// send request
$ch = curl_init($cross_site_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
$response = json_decode($response);

// do something with the response