我有導致數組的URL http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132。從外部數組/ API/URL使用PHP獲取信息
我想要得到的第一個「sellorders
」在這種情況下是值:0.00000048並將其存儲在變量$sellorderprice
。
任何人都可以幫忙嗎?
謝謝。
我有導致數組的URL http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132。從外部數組/ API/URL使用PHP獲取信息
我想要得到的第一個「sellorders
」在這種情況下是值:0.00000048並將其存儲在變量$sellorderprice
。
任何人都可以幫忙嗎?
謝謝。
只需通過file_get_contents
訪問url內容。你的網頁實際上是返回一個JSON字符串,來獲取這些值轉換爲有意義的數據,它直通json_decode
解碼,該訪問後相應所需要的數據:
$url = 'http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132';
$data = json_decode(file_get_contents($url), true);
$sellorderprice = $data['return']['DOGE']['sellorders'][0]['price'];
echo $sellorderprice;
該代碼實際上直接指向指數爲零0
其獲得的第一個價格。如果您需要獲得所有項目的只是簡單地迴應他們都需要迭代的所有項目通foreach
:
foreach($data['return']['DOGE']['sellorders'] as $sellorders) {
echo $sellorders['price'], '<br/>';
}
它的簡單,你只需要像這樣的解碼JSON:
$json = file_get_contents("http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132"); $arr = json_decode($json, true); $sellorderprice = $arr['return']['DOGE']['sellorders'][0]['price'];
使用json_decode嘗試($輸出);它會將json字符串轉換爲數組格式。 – Daenarys