您好我正在尝试使用此url从维基百科进行此api调用,但是当我转储变量时它表示它为null。这个功能适用于我的其他json api调用但不是这个,我在broswer手动测试它给我一个结果。这是我的尝试
$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;
}
答案 0 :(得分:1)
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;
}
答案 1 :(得分:0)
您应该考虑使用http_build_query()
来构建网址:
$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',
));