2015-05-09 70 views
2

我試圖讓從ULR一個JSON,但我得到的錯誤:的file_get_contents(JSON)不工作有效網址

警告:的file_get_contents(http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=StatTrak™五十七|銅銀河(新廠)):未能打開流:HTTP請求失敗! HTTP/F中1.0 500內部服務器錯誤:\ Bitnami \ htdocs中\ Dreamweaver中\ freehtml5streets \ updateInventory.php上線70

這是我試圖用,因爲你可以看到,如果你訪問的URL,它的作品(你將不得不復制整個事情):

http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=StatTrak™Five-SeveN |銅銀河(新廠)

我一直訪問類似的網址,並獲得JSONs,例如:

http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=AK-47%20%7C%20Redline%20%28Field-Tested%29

這些URL basicly相同的,但第一個沒有HTML標記。這裏是我的代碼:

 $data = file_get_contents('http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=' . $mydata->market_hash_name); 
     $json = json_decode($data); 

$mydata->market_hash_name讓我在網址的結尾部分,但沒有HTML標記(%20%)等

我怎樣才能得到這個工作?

+2

它看起來像你需要urlencode($ mydata-> market_hash_name)' – SuperJer

+0

這工作,謝謝! – Mitch8910

+0

我已經添加了這個效果的答案。 – SuperJer

回答

1

看來您需要urlencode您的$mydata->market_hash_name,因爲它使用了一些特殊字符和保留字符。下面應該工作:

//Assuming $mydata->market_hash_name == "StatTrak™ Five-SeveN | Copper Galaxy (Factory New)" 

$data = file_get_contents('http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=' . urlencode($mydata->market_hash_name)); 
$json = json_decode($data); 
0

您可能會發現一些領域,你是無法用這個方法(的file_get_contents),因爲遠程服務器期待例如一個有效用戶代理來檢索URL - 在我看來這是更好地使用cURL來獲取遠程頁面的內容....

相關問題