Using PHP, how do I get the first paragraph of a W

2020-05-03 10:30发布

How do I use PHP to get the first paragraph of any article from Wikipedia through their MediaWiki API?

I am open to all suggestions. Most probably CURL or XML will come in handy.

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-05-03 10:48

You can use the API as so:

http://en.wikipedia.org/w/api.php?action=parse&page=Stack_overflow&format=xml&prop=text&section=0

This will return an xml file with structure:

<?xml version="1.0"?>
<api>
  <parse title="Article Title">
    <text xml:space="preserve">Text you wanted goes here</text>
  </parse>
</api>

Note the variables: page=Article_Title_Goes_Here format=xml prop=text

查看更多
成全新的幸福
3楼-- · 2020-05-03 11:06

I would use file_get_contents('http://wikipedia.com/'.$rest_of_url)

Then just use string parsing to select everything form

to

http://php.net/manual/en/function.substr.php

查看更多
登录 后发表回答