我有一个返回以下内容的 CURL: {"data":{"base":"BTC","currency":"USD","amount":"9342.29"}}我正在尝试将数量 JSON 变量转换为 PHP 变量。我正在使用以下尝试:$result=curl_exec($ch);//1var_dump(json_decode($result));//2var_dump(json_decode($result, true));//3$data = json_decode($result[0]->data,true);之前的 var_dumps 的响应是 //1 /home/usbanktech/public_html/bitcoin2.php:24:int 1 //2 /home/usbanktech/public_html/bitcoin2.php:26:int 1 //3 $data attempt returns nothing试图将 "amount":"9342.29" 放入像 $amount 这样的 php 中。完整代码:// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/$ch = curl_init();curl_setopt($ch, CURLOPT_URL, "https://api.coinbase.com/v2/prices/BTC-USD/buy");//curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"name\": \"New receive address\"}");$headers = array();$headers[] = "Content-Type: application/json";$headers[] = "Authorization: Bearer abd90df5f27a7b170cd775abf89d632b350b7c1c9d53e08b340cd9832ce52c2c";//$headers[] = "CB-ACCESS-KEY: <your api key>";//$headers[] = "CB-ACCESS-SIGN: <the user generated message signature>";//$headers[] = "CB-ACCESS-TIMESTAMP: <a timestamp for your request>";curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);$result=curl_exec($ch);// Closingcurl_close($ch);$json = $result;$array = json_decode($json,1);$amount = $array['data']['amount'];echo $amount;
2 回答
米琪卡哇伊
TA贡献1998条经验 获得超6个赞
您需要将其设置为 true 才能获得 json:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
否则,您将获得一个整数作为返回值。
- 2 回答
- 0 关注
- 167 浏览
添加回答
举报
0/150
提交
取消