Question

I would like to use wikipedia api to get a page via php and curl. I read the instruction on http://www.mediawiki.org/wiki/API:Main_page and my example is similar.

The purpose is to automatically fetch movies infos given the title.

function getWikipediaFilmUrl($filmName) {
    $apiurl = "http://it.wikipedia.org/w/api.php";

    $parameters['format'] = "json";
    $parameters['prop'] = "revisions";
    $parameters['rvprop'] = "content";
    $parameters['action'] = "query";
    $parameters['titles'] = rawurlencode(ucwords($filmName));
    $parameters['redirects'] = "";

    $fields_string = "";
    $url = "";
    foreach ($parameters as $key => $value) {
        $fields_string[] = $key . '=' . $value;
    }
    $url = $apiurl . '?' . implode('&', $fields_string);

    //debug
    print_r($parameters);
    echo "<br /><a href='$url'>Resulting URL: $url</a><hr />";
    //

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, FALSE);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_REFERER, "");
    curl_setopt($ch, CURLOPT_HEADER, false);

    $result = curl_exec($ch);
    curl_close($ch);

    echo $result;
}

getWikipediaFilmUrl("main page");

This code is supposed to get the main page. The resulting URL is http://it.wikipedia.org/w/api.php?format=json&prop=revisions&rvprop=content&action=query&titles=Main%20Page&redirects= If you try to open it with the browser it works but fetched by curl return a page with a generic error and a text documentation about api usage...

I just tryied to copy & paste the resulting url in the curl call and it works .... why on earth $url does'nt work but does it's content ?!?.

Was it helpful?

Solution

Change

$url = $apiurl . '?' . implode('&amp;', $fields_string);

to

$url = $apiurl . '?' . implode('&', $fields_string);

You don't need to escape these ampersands since you aren't outputting them into HTML, but just feeding them straight into curl.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top