Envato API Curl response error, malformed token

Asked

Viewed 25 times

0

I’m trying to get data from Themeforest templates using the API of Envato; need data as template name and price from product ID. But the answer via Curl is that there is an error in the formation. I cannot identify the problem, my experience with Curl, JSON and Apis is limited.

What am I wearing:

$url = 'https://api.envato.com/v1/market/item-prices:'. $theme_id .'.json';
$personal_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

if (extension_loaded('curl') === true) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36");
    curl_setopt($ch, CURLOPT_HEADER, array('Authorization: Bearer '.$personal_token));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    
    $data = curl_exec($ch);
    $err_status = curl_error($ch);
    
    if(curl_exec($ch) === false) {                
        $err = 'Erro: ' . curl_error($ch);
        curl_close($ch);
        echo $data;
    }
    else {
        curl_close($ch); 
        echo 'Completo sem erros: ';
        print_r($data);
    }       
}

The exact answer is:

"error": "Missing or malformed API token: You need to provide an API token to access the API. The token must be a 32 Character long alphanumeric string.

1 answer

0

Okay, I found the answer: CURLOPT_HTTPHEADER instead of CURLOPT_HEADER.

The final and working code:

if (extension_loaded('curl') === true) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // remover em produção
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // remover em produção
    
    $data = curl_exec($ch);     
    if(curl_exec($ch) === false) {
        curl_close($ch);
        $err = 'Curl erro: ' . curl_error($ch);         
        echo $data;
    }
    else {
        curl_close($ch); 
        $json = json_decode($data, true);
        echo 'Title: '. $json['name'] .'<br>';
        echo 'Price: '. $json['id'] .'<br>';
        print("<pre>".print_r($data,true)."</pre>"); // DEBUG
    }   
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.