[英]How to take selected value from json response laravel?
我想使用 coinmarketcap 的 API 將 btc 的價格轉換為美元。
$response = curl_exec($curl); // Send the request, save the response
$type = json_decode($response,true);
獲得 200 Ok 響應
"data": {
"symbol": "BTC",
"id": "1",
"name": "Bitcoin",
"amount": 50,
"last_updated": "2018-06-06T08:04:36.000Z",
"quote": {
"USD": {
"price": 284656.08465608465,
"last_updated": "2018-06-06T06:00:00.000Z"
},
我想從 usd 獲取價格值,但是當我嘗試時它不起作用
$examount = $type->USD->price;
由於您正在使用
$response = curl_exec($curl); // Send the request, save the response
$type = json_decode($response,true);
這意味着您的對象被轉換為關聯數組,以訪問價格使用:
$type['data']['quote']['USD']['price'];
你應該很高興去。
編輯如果$type
包含多個元素,您將需要循環它們。
foreach($type['data'] as $singleQuote){
$price = $singleQuote['quote']['USD']['price'];
echo 'Quote for: '.$singleQuote['symbol'].' in USD: '.$price.'<br/>';
}
因為您正在傳遞第二個參數true
將其轉換為關聯數組:
$response = curl_exec($curl); // Send the request, save the response
$type = json_decode($response,true);
$examount = $type['data']['quote']['USD']['price'];
我希望以上內容可以根據 coinmarketcap 的文檔( https://coinmarketcap.com/api/documentation/v1/#section/Endpoint-Overview )解決您的問題
問題是由 $request 返回 null 引起的上述所有內容也是正確的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.