从维基百科 API 中提取数据 [英] Extracting data from Wikipedia API

查看:32
本文介绍了从维基百科 API 中提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用 json 从维基百科中提取标题和描述.所以......维基百科不是我的问题,我是 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;
?>

只是为了更容易点击:

我知道我可能只是做错了一件小事,但它真的让我很烦恼,而且代码......我习惯使用 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];

您可能收到此警告:

 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;

这篇关于从维基百科 API 中提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆