简体   繁体   中英

cant get values from returned string in CURL with cakephp

I simply have a returned string? from CURL and cannot get the values like access_token? I am using cakephp 4 (we have debug not var_dump)

Code in cakephp (same as php)

 $AccessToken = $this->CreateAccessToken($jwt);
 $token=json_decode($AccessToken);
  debug($token);
 debug($token['access_token']);

The string appears like below from the debug this but I can't extract the values. The issue I believe is the json_decode isnt working so i still get the string

/////////////////
debug($token) :        { "access_token": "Bearer exxxxxxxxxx1h7kQuxaOqB-AbaoMPh7qLgA", "expires_in": 1892160000, "api_domain": "https://xx1.xxh.com/v1", "token_type": "Bearer" }

debug($token['access_token']:Notice (8): Trying to access array offset on value of type int
/////////////

Thats what appears above on var_dump of debugs. I can display the whole returned token but i cant access anything inside it

here is the code that does that

private function CreateAccessToken($jwt)
{
$jwt_token = "Bearer ".$jwt;
$headers = array("content-type: application/json", "Authorization:".$jwt_token);

    $curl = curl_init();
    curl_setopt_array($curl, array(
    CURLOPT_URL => "https://xx/v1/xx/api/token",
    CURLOPT_SSL_VERIFYHOST => 0,
    CURLOPT_SSL_VERIFYPEER => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_HTTPHEADER => array("content-type: application/x-www-form-urlencoded"),
    CURLOPT_POSTFIELDS => http_build_query(array(
    'assertion' => $jwt_token,
    'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer')),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);
    if($err) 
    {
      echo "cURL Error #:" . $err;
      return 0;
    } 
    else 
    {
      return $response;
    }
}

output var_dump($token)//still a string in an int?

{ "access_token": "Bearer exxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx2GB3PS5c4tq9iOvSTH7lx-7gI_JHaw", "expires_in": 1892160000, "api_domain": "https://xx.xx.com/v1", "token_type": "Bearer" }int(1) 

output var_dump($AccessToken)//still a string in a bool?

 { "access_token": "Bearer eyxxxxxxnx1bul67sSWM8fxFmLkJywP6UZryO9o8", "expires_in": 1892160000, "api_domain": "https://xx.xx.com/v1", "token_type": "Bearer" }bool(true) 

How to extract required data from CURL response?

In the function to send Curl you need to add curlopt_returntransfer as this allows a string tobe returned and not an integer

private function CreateAccessToken($jwt)
........
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //ADD
$response = curl_exec($curl);

Then just access the token

In cakephp

$AccessToken = $this->CreateAccessToken($jwt);
$token=json_decode($AccessToken);


debug($token->access_token);
//this works as i can access the access token field

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM