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

标签 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 请求失败! 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,例如:

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 获取 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/

相关文章:

PHP函数mysqli_bind_param参数错误

php - 如何在 Guzzle 中捕获 cURL 使用的 IP?

php - 在函数参数中使用单管道 '|' 有什么作用?

javascript - 在 Retina iPhone 上正确设置设备宽度和比例? CSS

javascript - Jquery 不将 html 传递到 iframe

php - jQuery .click 函数每次都会执行吗?

javascript - 自定义 Google 搜索不会出现在 IE8 和 IE9 上

javascript - JS如何将json转换为数组

json - 未处理的异常:类型 '_InternalLinkedHashMap<String, dynamic>'不是类型转换中类型 'List<dynamic>'的子类型吗?

ios - json 解析时无法使用 Codable Concept swift 4 管理 nil