Jayaram
Jayaram

Reputation: 839

Wikipedia Curl request error

I am trying to get the summary of a page from Wikipedia using Curl. My code is

<?php

$title = <input from user>;

$url = 'http://en.wikipedia.org/w/api.php?action=query&titles='.$title.'&format=xml&prop=revisions&rvprop=content&redirects';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_USERAGENT, 'testing wikipedia services');
$result = curl_exec($curl);
print $result;

//$content = $c->query->pages->page->revisions->rev;
//print $content;
?>

I initially want to test the code to see if my code is returning the whole page. But I get an error which says

Request: [unknown method] http://en.wikipedia.org:80/w/api.php?action=query&titles=US Environmental Protection Agency&format=xml&prop=revisions&rvprop=content&redirects, from 161.80.8.53 via cp1005.eqiad.wmnet (squid/2.7.STABLE9) to [unknown host] ([unknown]) Error: ERR_INVALID_URL, errno [No Error] at Tue, 04 Dec 2012 16:55:52 GMT

The URL works, But it still generates the unknown method error!

Upvotes: 2

Views: 617

Answers (2)

lafor
lafor

Reputation: 12776

You need to urlencode($title) in your URL.

Upvotes: 3

Sorin Trimbitas
Sorin Trimbitas

Reputation: 1497

You forgot to encode the $title variable. Example :

str_replace(' ', '_', $title);

Upvotes: 1

Related Questions