How to insert only one value from curl array in a variable? - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Saturday, October 1, 2022

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

** 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);

?>


source https://stackoverflow.com/questions/73880539/how-to-insert-only-one-value-from-curl-array-in-a-variable

No comments:

Post a Comment