2016-06-27 100 views
-1

我想要獲取JSON中變量的值,但它會一直給我提供未定義索引,其中索引絕對存在於我打開URL時。從curl獲取特定數據

這裏是我的代碼:

//initializing cURL 
      $ch = curl_init(); 
//setting the URL 
      $priceURL = 'http://steamcommunity.com/market/priceoverview/?currency=1&appid=570&market_hash_name=Exalted%20Manifold%20Paradox'; 
//set options 
      curl_setopt($ch, CURLOPT_URL, $priceURL); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
      curl_setopt($ch, CURLOPT_HEADER, 0); 
//execute 
      $string1 = curl_exec($ch); 
//close curl session/free resources 
      curl_close ($ch); 
//decode the json string into an array 
      $json_price_file = json_decode($string1, true); 
      $price = $json_price_file ["lowest_price"]; 

它給未定義指數的錯誤:LOWEST_PRICE起。但是,當您打開網址時,該變量肯定存在。我錯過了什麼?請幫助:D

+1

post $'string1'有什麼? –

+0

$ string1只是我在那裏創建的一個變量,所以我可以將它傳遞給json_decode。 – KMackinley

+0

您的代碼適用於我。請檢查你有'php_curl'的擴展名。 – wazelin

回答

0

我確認你的代碼運行良好,就像Wazelin說的那樣,在php.ini中檢查你的php_curl。

編輯:看你的代碼(通過電子郵件)後,必須補充一點:當你拿到$market_hash_name值則需要更換由20%(有效的URL)的空白

$market_hash_name = str_replace(' ', '%20', $market_hash_name); 

那我勸你添加一個條件爲LOWEST_PRICE的,以避免錯誤:

if (isset($json_price_file["lowest_price"])) { 
    #your code... 
} 

問候。

+0

它給了什麼價值? – KMackinley

+1

,給我︰$ 26.16 – Peacefull

+0

如果可以,我可以將您的整個代碼發送到您的電子郵件嗎?可能是問題在其他地方 – KMackinley