我只是从CURL返回了一个字符串,但无法获得access_token之类的值。我使用的是cakephp 4(我们使用的是debug,而不是var_dump)
cakephp中的代码(与php相同)
$AccessToken = $this->CreateAccessToken($jwt);
$token=json_decode($AccessToken);
debug($token);
debug($token['access_token']);
这个字符串看起来像下面的调试,但是我不能提取值。我认为问题是json_decode不工作,所以我仍然得到字符串
/////////////////
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
/////////////
这就是上面调试的var_dump上显示的内容。我可以显示整个返回的令牌,但不能访问它里面的任何内容
下面是执行此操作的代码
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)//仍然是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)//仍然是bool中的字符串吗?
{ "access_token": "Bearer eyxxxxxxnx1bul67sSWM8fxFmLkJywP6UZryO9o8", "expires_in": 1892160000, "api_domain": "https://xx.xx.com/v1", "token_type": "Bearer" }bool(true)
1条答案
按热度按时间9lowa7mx1#
我需要将api字符串保存到db的文本字段中,然后使用json_decode将其读回,以获取访问令牌的各个部分。