Home > Net >  How to insert only one value from curl array in a variable?
How to insert only one value from curl array in a variable?

Time:09-29

** I want to insert only a specific field of my curl token:**

I have a variable that will receive this token called "$r" and I use foreach to capture the value, but I'm not getting it:this is the answer

{
"access_token":"<TOKEN RESPONSE HERE >", 
"token_type": "bearer", 
"expires_in": 14400
}

Below is my code

<?php


// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.dropbox.com/oauth2/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "refresh_token=<REFRESH HERE>&grant_type=refresh_token&client_id=<KEY HERE>&client_secret=<SECRET HERE>");
$body = curl_exec($ch);
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);



if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

else{

    if (is_array($result) || is_object($result))
{
    foreach ($result as $r) {
        echo  $r['access_token'];
    }
}
    echo 'Foi: '.$result  ;
}
curl_close($ch);

?>

CodePudding user response:

curl_exec returns a string. Convert it with json_deode($result,1) to an associative array

  • Related