"test_id", -6ren">
gpt4 book ai didi

php - 使用 PHP 回显 JSON 响应

转载 作者:行者123 更新时间:2023-12-03 18:45:39 28 4
gpt4 key购买 nike

我正在尝试从 Plaid API 回显我的 JSON 响应的一部分。

代码如下:

$data = array(
"client_id"=>"test_id",
"secret"=>"test_secret",
"public_token"=>"test,fidelity,connected");
$string = http_build_query($data);

//initialize session
$ch=curl_init("https://tartan.plaid.com/exchange_token");

//set options
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//execute session
$exchangeToken = curl_exec($ch);
echo $exchangeToken;
$exchangeT = json_decode($exchangeToken);
echo $exchangeT['access_token'];
//close session
curl_close($ch);

这是响应:

{ "sandbox": true, "access_token": "test_fidelity" }

我还收到 500 内部服务器错误,这是 echo $exchangeT 行的结果。我想获取 JSON 响应的 access_token 部分,对其进行回显以进行验证,并最终将其保存到数据库中。

最佳答案

如果您在 json_decode 中将 true 作为第二个参数 传递,那么您将得到一个 array 而不是 对象 所以改变这个

     $exchangeT = json_decode($exchangeToken, true);
// it wii output as $exchangeT = array('sandbox'=>true,'access_token'=>'test_fidelity');
echo $exchangeT['access_token'];

有关更多信息,请阅读 http://php.net/manual/en/function.json-decode.php

关于php - 使用 PHP 回显 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38504292/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com