0

I'm trying to connect to this webpage of the steam api. The market_hash_name parameter is the item I want to search. And as you can see, the response is in the format json. My code is this one:

$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";
}

Sometimes I get the correct response and everything, but sometimes I get this characters <Ê+ÍÉÿÿOüË% as the $data variable. But it still gets inside the if! So it does have the 'success' key in that JSON response, although it's not readable.

EDIT: So I've seen that sometimes the url decides to return null instead of the JSON response. I think I get this weird characters when I get the null return. But this doesn't explain why It enters to the if sentence :/

lpares12
  • 3,504
  • 4
  • 24
  • 45

1 Answers1

0

You need add this to your CURL:

curl_setopt($ch, CURLOPT_ENCODING, '');

See the explanation here : PHP curl returning strange characters