file_get_contents(JSON)不适用于有效的URL

时间:2015-05-09 14:02:00

标签: php html json decode

我正在尝试从ULR获取JSON,但是我收到错误:

警告:file_get_contents(http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=StatTrak™Five-SeveN | Copper Galaxy(崭新出厂)):无法打开流:HTTP请求失败!第70行的F:\ Bitnami \ htdocs \ Dreamweaver \ freehtml5streets \ updateInventory.php中的HTTP / 1.0 500内部服务器错误

这是我正在尝试使用的网址,因为您可以看到,如果您访问它,它可以工作(您必须复制整个内容):

http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=StatTrak™Five-SeveN |铜银河(新工厂)

我一直在访问类似的URL并获取JSON,例如:

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

这些网址基本相同,但第一个网址没有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 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

您可能会发现某些域无法使用此方法(file_get_contents)来检索URL,因为远程服务器正在等待有效的useragent - 我认为最好使用cURL来获取内容一个远程页面......