bigjoy10
bigjoy10

Reputation: 55

Decode G2A search results (json) to array

Help me to grab name of games from url

This page output json format. I try to convert it to array, but my code not work. Please help me!

$url = 'https://search.g2a.com/items/select?json.wrf=jQuery111003403934023808688_1411464896728&q=NOT+type%3Aindividual+AND+(-type%3Agaming+AND+wholesaleQty%3A%5B1+TO+*%5D+AND+wholesaleMinPrice%3A%5B0+TO+198%5D)&wt=json&start=0&rows=10000&sort=sortOrder+DESC&_=1411464896757';
$content = file_get_contents($url);
$json = json_decode($content, true);

echo "<pre>";
print_r($json);
echo "</pre>";

Upvotes: 1

Views: 823

Answers (2)

jedwards
jedwards

Reputation: 30210

The output from that URL (that I get currently starts with):

jQuery111003403934023808688_1411464896728({"responseHeader" ...

This isn't a pure JSON response, but rather a JSONP response.

If you're just trying to parse it in PHP, maybe something like:

$url = ...;  // Your URL Here
$data = file_get_contents($url);
$pos  = strpos($data, '{');
$data = substr($data, $pos, strlen($data) - $pos - 2);
$json = json_decode($data, true);

echo "<pre>";
print_r($json);
echo "</pre>";

Upvotes: 1

Ja͢ck
Ja͢ck

Reputation: 173542

You should remove the JSONP parameter json.wrf from the URL first:

https://search.g2a.com/items/select?q=NOT+type%3Aindividual+AND+(-type%3Agaming+AND+wholesaleQty%3A%5B1+TO+*%5D+AND+wholesaleMinPrice%3A%5B0+TO+198%5D)&wt=json&start=0&rows=10000&sort=sortOrder+DESC&_=1411464896757

This will return a proper JSON result.

Upvotes: 4

Related Questions