Curl + php steam appi,响应错误

时间:2015-12-05 10:56:29

标签: php json curl steam-web-api

我试图连接到{api的this webpagemarket_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!所以它确实取得了成功&#39;关键在于JSON响应,尽管它不可​​读。

编辑:所以我发现有时网址决定返回null而不是JSON响应。当我得到null返回时,我想我得到了这个奇怪的字符。但这并没有解释为什么它进入if句子:/

1 个答案:

答案 0 :(得分:0)

您需要将此添加到您的CURL:

curl_setopt($ch, CURLOPT_ENCODING, '');

请参阅此处的说明:PHP curl returning strange characters