我使用下面的curl代码来获取和显示json文件内容,一切正常,下面的代码
当我用
打印json结果时$json = json_decode($response);
print_r($json);
以这种格式显示结果
stdClass Object ( [id] => 123
[food] => Array ( [0] => rice )
[buyer] => AkwaezeOby
[amount] => 2000 )
现在我正在努力获得食品和买家的价值,我在下面尝试了这个,但无法让它发挥作用
print_r($json["food"][0]);
print_r($json["buyer"]);
or
echo $json["food"][0];
echo $json["buyer"];
以下是数据显示的工作代码。有人可以帮助我获得食物和买家的价值观。感谢
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "myapi.com",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
//CURLOPT_POSTFIELDS => "$data",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"authorization: --my number goes here--",
"content-type: application/json; charset=utf-8"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$json = json_decode($response);
print_r($json);
答案 0 :(得分:1)