问题描述
我希望能够使用json从Wikipedia中提取标题和描述.所以...维基百科不是我的问题,我是json的新手,并且想知道如何使用它.现在我知道有成百上千的教程,但是我已经工作了几个小时,它什么也没显示,这是我的代码:
I would like to be able to extract a title and description from Wikipedia using json. So... wikipedia isn't my problem, I'm new to json and would like to know how to use it. Now I know there are hundreds of tutorials, but I've been working for hours and it just doesn't display anything, heres my code:
<?php
$url="http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects&inprop=url";
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
$pageid = $data->query->pageids;
echo $data->query->pages->$pageid->title;
?>
只需单击它即可更轻松:
Just so it easier to click:
我知道我可能做错了一点小事,但是它确实困扰着我,还有代码……我习惯于使用xml,并且我已经做了很多切换,所以您可以解释一下这对我和未来的访问者来说有点麻烦,因为我很困惑……您需要我没说的任何东西,只需评论一下,确定可以得到,谢谢,谢谢!
I know I've probably just done a tiny thing wrong, but its really bugging me, and the code... I'm used to using xml, and I have pretty much just made the switch, so can you explain it a bit for me and for future visitors, because I'm very confused... Anything you need that I haven't said, just comment it, im sure I can get it, and thanks, in advance!
推荐答案
$pageid
返回带有一个元素的数组.如果您只想握拳,那您应该这样做:
$pageid
was returning an array with one element. If you only want to get the fist one, you should do this:
$pageid = $data->query->pageids[0];
您可能会收到以下警告:
You were probably getting this warning:
Array to string conversion
完整代码:
$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects&inprop=url&indexpageids';
$json = file_get_contents($url);
$data = json_decode($json);
$pageid = $data->query->pageids[0];
echo $data->query->pages->$pageid->title;
这篇关于从Wikipedia API提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!