【发布时间】:2015-12-05 10:56:28
【问题描述】:
我正在尝试连接到 Steam api 的 this webpage。 market_hash_name 参数是我要搜索的项目。如您所见,响应采用 json 格式。我的代码是这个:
$url = "http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=" . urlencode($hash_name);
$ch = curl_init();
$timeout = 10;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
$data = json_decode($data, true);
if ($data['success'] == true) {
echo $url . "<br>";
echo "Data Found: " . $data;
} else {
"Data not found";
}
有时我得到正确的响应和一切,但有时我得到这个字符 <Ê+ÍÉÿÿOüË% 作为 $data 变量。但它仍然进入 if!所以它在 JSON 响应中确实有“成功”键,尽管它不可读。
编辑:所以我看到有时 url 决定返回 null 而不是 JSON 响应。我想当我得到null 返回时,我得到了这个奇怪的字符。但这并不能解释为什么它进入 if 语句:/
【问题讨论】:
-
我觉得不错
-
对我来说也是如此,即使我用我的网络浏览器手动检查它确实会显示适当的结果,我不明白为什么有时我会得到这个奇怪的字符。
标签: php json curl steam-web-api