如何从希伯来语维基百科中获取数据?

时间:2018-01-08 06:20:02

标签: php wikipedia

我找到了很多代码来从en.wikipedia.org获取数据 但我没有找到如何从希伯来语Wikiedia获取数据.. 我试试这个:

$query = $_GET['query'];
$url = 'https://he.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles='.urlencode($query).'&format=json&utf8=';

$json = file_get_contents($url);
$data = json_decode($json);

$pageid = $data->query->pageids[0];
echo $data->query->pages->$pageid->title;
echo $data->query->pages->$pageid->extract;

我得到了这个错误:

 Notice: Undefined property: stdClass::$pageids in C:\xampp\htdocs\wiki.php on line 37

 Notice: Undefined property: stdClass::$ in C:\xampp\htdocs\wiki.php on line 38

 Notice: Trying to get property of non-object in C:\xampp\htdocs\wiki.php on line 38

 Notice: Undefined property: stdClass::$ in C:\xampp\htdocs\wiki.php on line 39

 Notice: Trying to get property of non-object in C:\xampp\htdocs\wiki.php on line 39

var_dump($data)

的输出
object(stdClass)#1 (2) { 
    ["batchcomplete"]=> string(0) "" 
    ["query"]=> object(stdClass)#4 (1) { 
        ["pages"]=> object(stdClass)#3 (1) { 
            ["899083"]=> object(stdClass)#2 (11) { 
                ["pageid"]=> int(899083) 
                ["ns"]=> int(0) 
                ["title"]=> string(8) "גוגל" 
                ["extract"]=> string(47)
            }
        }
    }
}

看起来它没有读取代码.. 请帮忙。感谢

1 个答案:

答案 0 :(得分:0)

根据var_dump($data)的输出,您可以像这样访问第一页:

$pages = (array) $data->query->pages;
$page = reset($pages);

或者,您可以遍历多个页面,如下所示:

$pages = (array) $data->query->pages;

foreach ($pages as $page) {
    var_dump($page);
}

将单个页面作为变量$page后,您可以像以前一样访问其中的属性。

echo $page->title;
echo $page->extract;