$wiki_img = "http://en.wikipedia.org/w/api.php?action=query&titles=Google&prop=pageimages&format=json&pithumbsize=500";
$json2 = file_get_contents($wiki_img);
$data2 = json_decode($json2, TRUE);
foreach ($data2['query']['pages'] as $pages)
{
print_r($pages['source']);
}
我的代码无效。
但我无法找到错误。
我收到了这个错误
"注意:未定义的索引:C:\ xampp \ htdocs中的来源\"
答案 0 :(得分:0)
您获得的JSON数据是
{
query: {
pages: {
1092923: {
pageid: 1092923,
ns: 0,
title: "Google",
thumbnail: {
source: "http://upload.wikimedia.org/wikipedia/commons/thumb/0/0e/Googleplex-Patio-Aug-2014.JPG/500px-Googleplex-Patio-Aug-2014.JPG",
width: 500,
height: 375
},
pageimage: "Googleplex-Patio-Aug-2014.JPG"
}
}
}
}
应该是......
foreach($data2['query']['pages'] as $value){
echo $value['thumbnail']['source'];
}
输出:
http://upload.wikimedia.org/wikipedia/commons/thumb/0/0e/Googleplex-Patio-Aug-2014.JPG/500px-Googleplex-Patio-Aug-2014.JPG