Fetch the description from wikipedia from an article

独自空忆成欢 提交于 2019-12-12 02:17:59

问题


I am trying to make a API call to wikipedia through: http://en.wikipedia.org/w/api.php?action=parse&page=Petunia&format=xml, but the xml is full with html and css tags.

Is there a way to fetch only plain text without tags? Thanks!

*Edit 1:

$json = json_decode(file_get_contents('http://en.wikipedia.org/w/api.php?action=parse&page=Petunia&format=json'));
$txt  = strip_tags($json->text);
var_dump($json);

Null displayed.


回答1:


Question was partially answered here

$url = 'http://en.wikipedia.org/w/api.php?action=parse&page=Petunia&format=json&prop=text';
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, "TestScript"); // required by wikipedia.org server
$c = curl_exec($ch);

$json = json_decode($c);

var_dump(strip_tags($json->{'parse'}->{'text'}->{'*'}))

I was not able to use file_get_contents but it works fine with cURL.




回答2:


it is possible to fetch info or description from wikipedia by using xml.

       $url = "http://en.wikipedia.org/w/api.php?action=opensearch&search=".$term."&format=xml&limit=1";
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
        curl_setopt($ch, CURLOPT_POST, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, false);   // Include head as needed
        curl_setopt($ch, CURLOPT_NOBODY, FALSE);        // Return body
        curl_setopt($ch, CURLOPT_VERBOSE, FALSE);           // Minimize logs
        curl_setopt($ch, CURLOPT_REFERER, "");            // Referer value
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    // No certificate
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);     // Follow redirects
        curl_setopt($ch, CURLOPT_MAXREDIRS, 4);             // Limit redirections to four
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);     // Return in string
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; he; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8");   // Webbot name
        $page = curl_exec($ch);
        $xml = simplexml_load_string($page);
        if((string)$xml->Section->Item->Description) {
            print_r(array((string)$xml->Section->Item->Text, 
            (string)$xml->Section->Item->Description, 
            (string)$xml->Section->Item->Url));
        } else {
            echo "sorry";
        } 

But curl must be install on server... have a nice day...



来源:https://stackoverflow.com/questions/8533388/fetch-the-description-from-wikipedia-from-an-article

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!