帮我从网址中获取游戏名称
此页面输出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>";
答案 0 :(得分:4)
您应首先从网址中删除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结果。
答案 1 :(得分:1)
该网址的输出(我目前开始使用):
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>";