views:

165

answers:

2

Hi guys, I noticed there was a question somewhat similar to mine, only with c#:link text. Let me explain: I'm very new to the whole web-services implementation and so i'm experiencing some difficulty understanding (especially due to the vague mediawiki api manual).

I want to retrieve the entire page as a string in PHP (xml file) and then process it in PHP (i'm pretty sure there are other more sophisticated ways to parse xml files but w/e): Main Page wikipedia.

I tried doing $fp = fopen($url,'r'); it outputs: HTTP request failed! HTTP/1.0 400 Bad Request. the api does not require a key to connect to it.

Can you describe in detail how to connect to the API and get the page as a string?

Thank you.

EDIT: the url is this: $url='http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles=Main Page'; I simply want to read the entire content of the file into a string to use it.

A: 

Connecting to that API is as simple as retrieving the file

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

$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles=Main%20Page';
$fp = fopen($url, 'r');
while (!feof($fp)) {
  $c .= fread($fp, 8192);
}
echo $c;

file_get_contents http://www.php.net/manual/en/function.file-get-contents.php

$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles=Main%20Page';
$c = file_get_contents($url);
echo $c;

The above two can only be used if your server has teh fopen wrappers enabled

otherwise if your server has curl installed you can use that

$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles=Main%20Page';
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$c = curl_exec($ch);
echo $c;
Galen
thank you so much! do you know how i can enable fopen wrappers on my local apache?
sombe
make sure allow_url_fopen = 1 in your php ini. http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
Galen
A: 

You probably need to urlencode the parameters that you are passing in the query string ; here, at least the "Main Page" requires encoding -- without this encoding, I get a 400 error too.

If you try this, it should work better (note the space is replaced by %20) :

$url='http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles=Main%20Page';
$str = file_get_contents($url);
var_dump($str);

With this, I'm getting the content of the page.


A solution is to use urlencode, so you don't have to encode yourself :

$url='http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles=' . urlencode('Main Page');
$str = file_get_contents($url);
var_dump($str);
Pascal MARTIN