将G2A搜索结果(json)解码为数组


Decode G2A search results (json) to array

帮助我从url 中获取游戏名称

此页面输出json格式。我试图将它转换为数组,但我的代码不起作用。请帮帮我!

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

您应该首先从URL中删除JSONP参数json.wrf

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

这将返回一个正确的JSON结果。

该URL的输出(我当前从中获得):

jQuery111003403934023808688_1411464896728({"responseHeader" ...

这不是一个纯粹的JSON响应,而是一个JSONP响应。

如果你只是想用PHP解析它,可能会有这样的东西:

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