Php JSON解码卷曲问题
嗨,我正试图使用这个url从wikipedia调用这个api,但是当我转储变量时它说它是空的。此函数适用于我的其他json api调用,但不适用于此,我在broswer中手动测试了它,它给出了一个结果。这是我的尝试Php JSON解码卷曲问题,php,json,Php,Json,嗨,我正试图使用这个url从wikipedia调用这个api,但是当我转储变量时它说它是空的。此函数适用于我的其他json api调用,但不适用于此,我在broswer中手动测试了它,它给出了一个结果。这是我的尝试 $url = 'http://en.wikipedia.org/w/api.php?action=query&format=json&titles=Image:Romerolagus diazi (dispale) 001.jpg&prop=imageinfo
$url = 'http://en.wikipedia.org/w/api.php?action=query&format=json&titles=Image:Romerolagus diazi (dispale) 001.jpg&prop=imageinfo&iiprop=url';
$result = apicall($url);
var_dump($result);
function apicall($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'MyBot/1.0 (http://www.mysite.com/)');
$result = curl_exec($ch);
if (!$result) {
exit('cURL Error: '.curl_error($ch));
}
$var = json_decode($result);
return $var;
}
urlencode()
问题,如下修改
<?php
$url = 'http://en.wikipedia.org/w/api.php';
$titles = urlencode('Image:Romerolagus diazi (dispale) 001.jpg');
$queryStr = 'action=query&format=json&titles='.$titles.'&prop=imageinfo&iiprop=url';
$url = $url . '?' . $queryStr;
$result = apicall($url);
var_dump($result);
function apicall($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22');
$result = curl_exec($ch);
if (!$result) {
exit('cURL Error: '.curl_error($ch));
}
var_dump($result);
$var = json_decode($result);
return $var;
}
<代码> 您应该考虑使用URL:
$url = 'http://en.wikipedia.org/w/api.php?' . http_build_query(array(
'action' => 'query',
'format' => 'json',
'titles' => 'Image:Romerolagus diazi (dispale) 001.jpg',
'prop' => 'imageinfo',
'iiprop' => 'url',
));
“不工作”是什么意思?它会输出“我不工作”还是什么?