-1

I am attempting to parse the JSON from the URL bellow. However when I run the var_dump(json_decode($result, true)) it returns NULL. But when I copy the URL from the echo $item_url it returns the proper JSON.

Another issue I have read is the var_dump(json_decode($result, true)); is returning a string with spaces and that could be an issue

$item_url = "http://steamcommunity.com/market/priceoverview/?country=US&currency=1&appid=730&market_hash_name=" . $rgDescriptions->market_hash_name;

echo $item_url;

$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, urlencode($item_url));
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Your application name');
$result = curl_exec($curl_handle);
curl_close($curl_handle);

var_dump(json_decode($result, true));

Bellow is an example of a URL that may get passed.

http://steamcommunity.com/market/priceoverview/?country=US&currency=1&appid=730&market_hash_name=AK-47%20|%20Aquamarine%20Revenge%20(Minimal%20Wear)

Eric Goncalves
  • 5,253
  • 4
  • 35
  • 59

1 Answers1

1

its working for me after changing a line in your code. I think you need to encode $rgDescriptions->market_hash_name only not whole url.

Change

curl_setopt($curl_handle, CURLOPT_URL, urlencode($item_url));

TO

curl_setopt($curl_handle, CURLOPT_URL, $item_url);

Full Code

$item_url = "http://steamcommunity.com/market/priceoverview/?country=US&currency=1&appid=730&market_hash_name=AK-47%20|%20Aquamarine%20Revenge%20(Minimal%20Wear)";

$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $item_url);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Your application name');
$result = curl_exec($curl_handle);
curl_close($curl_handle);

var_dump(json_decode($result, true));

Output:

array(4) {
  ["success"]=>
  bool(true)
  ["lowest_price"]=>
  string(6) "$26.38"
  ["volume"]=>
  string(3) "133"
  ["median_price"]=>
  string(6) "$26.35"
}
Chetan Ameta
  • 7,696
  • 3
  • 29
  • 44