我正在尝试从 ULR 获取 JSON,但出现错误:
警告:file_get_contents( http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=StatTrak ™ Five-SeveN | Copper Galaxy(全新出厂)):无法打开流:HTTP 请求失败! F:\Bitnami\htdocs\Dreamweaver\freehtml5streets\updateInventory.php 第 70 行中的 HTTP/1.0 500 内部服务器错误
这是我正在尝试使用的 URL,如果您访问它,您会看到它有效(您必须复制整个内容):
http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=StatTrak ™ 五七 |铜银河 (崭新出厂)
我一直在访问类似的 URL 并获取 JSON,例如:
这些网址基本相同,但第一个网址没有 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
获取 URL 末尾的部分,但没有 HTML 标签 (%20%) 等。
我怎样才能让它工作?
最佳答案
您似乎需要对 $mydata->market_hash_name
进行 urlencode
,因为它使用了许多特殊的保留字符。以下应该有效:
//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);
关于php - file_get_contents(JSON) 不适用于有效的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30140816/