<?php
//$url = 'localhost/api/avanws.php';
$url = 'http://test.papelcero.es/api/avanws.php';
$data = array (
    "grant_type" => "client_credentials",
    "client_secret" => "9b87f76e-def1-4515-9a62-fcefb6a53095",
    "client_id" => "API-ESMDD_dev",
    "service" => "servToken",
    "code" => "8356e8922cdccb82gh538fu2"
);
$headers = [];
$result = curlPost($url,$data,$headers);
print_r($result);
function curlPost($url,$data,$headers) {
    $ch = curl_init();
    $timeout = 30;
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $response = curl_exec ($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close ($ch);
    return json_decode($response);
}
//$url = 'localhost/api/avanws.php/getDocs/202004010814108241';
//$url = 'http://test.papelcero.es/api/avanws.php/getDocs/202004221425575972';
$url = 'http://test.papelcero.es/api/avanws.php/getDocs/202004291724049959';
$headers = array(
    'authorization-token: Bearer ' . $result->servToken->client_token,
    'client-id: API-ESMDD_dev',
    'p0-api-key: re543876gfth3246jnjno095a' 
);
$result = curlGetData($url,$headers);
//===================================
//===================================
$ruta_doc = 'documento de prueba.pdf';
@unlink('documento de prueba.pdf');
$create_doc = fopen($ruta_doc, 'a');
fwrite($create_doc, base64_decode($result->document->doc_file));
fclose($create_doc);
//===================================
//===================================
print_r($result);
function curlGetData($url,$headers) {

    $ch = curl_init();
    $timeout = 30;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPGET,true);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

    $result = curl_exec($ch);
    curl_close($ch);
    return json_decode($result);
}

?>