Beste mensen,

Ik ben bezig om een systeem te maken met onderwerpen. En bij elk onderwerp het eerste alinea van Wikipedia te gebruiken. En de eerste <p> is ook meteen de eerste alinea van de tekst.

Weet iemand hoe dat moet?
Alvast bedankt voor de reacties.

Mvg,

Patrick
Wikipedia heeft een API. Kijk daar eens naar.
Aar ik heb gezocht er naar, maar kan het niet vinden. :(

Alleen een API om te kijken of die pagina bestaat.
Beste Frank,

ik heb nu dit:

<?php
ini_set('user_agent', 'MyCoolTool/1.1 (http://domeinnaam.com/lab/wikipedia.php; [email protected]) BasedOnSuperLib/1.4');
?>
<link rel="EditURI" type="application/rsd+xml" href="http://en.wikipedia.org/w/api.php?format=json&action=query&titles=Nederland&prop=revisions&rvprop=content" />


Alleen het werkt nier er komt niks in beeld.
Maar als ik de url in de browser stopt dan komt er wel wat te staan op de Wikipedia pagina.

Wat doe ik fout?
Beste Frank,

Ik heb hem nu bijna:

Dit is de code die ik tot nu heb:

<?php

class toIBAN
{
    private $obj;
    private $error;
    
    function __construct()
    {
        $this->error = false;
    }
    
    public function getError()
    {
        return $this->error;
    }
    
    public function getObject()
    {
        return $this->obj;
    }

    public function getTitle()
    {
        return $this->obj->query->pages->{296302}->revisions[0]->{'*'};
    }
    public function run($vraag)
    {     
        $serverurl = 'http://en.wikipedia.org/w/api.php?format=json&action=query&titles='. $vraag .'&prop=revisions&rvprop=content';
        
        if(!$json = @file_get_contents($serverurl))
        {
            $this->error = 'No data received, please make sure connection is working and requested API exists';
            return;
        }

        $this->obj = json_decode($json);
        
        if(isset($this->obj->error))
        {
            $this->error = $this->obj->error;
            return false;
        }
        
        return true;
    }

};

$iban = new toIBAN();

if($iban->run('Nederland'))
{
    //print_r($iban->getObject());
    
    echo '<br><br>';
    echo 'Title: '.$iban->getTitle().'<br>';
}
else
{
    echo $iban->getError();
}
?>


Dan komt dit op de site te staan:
Title: {{Wiktionary}} '''Nederland''' is the Dutch name for the '''[[Netherlands]]'''. It is also the name of several villages: * [[Nederland, Overijssel]], in Steenwijkerland, Netherlands * [[Nederland, Colorado]], United States * [[Nederland, Texas]], United States ==See also== * [[Netherlands (disambiguation)]] {{geodis}} [[it:Nederland]]

Hoe kan ik dit voor de mensen een mooie tekst van maken. En hoe moet ik de paginaID weten van elk pagina van wikipedia?

[size=xsmall]Toevoeging op 09/09/2013 19:53:24:[/size]

Ik heb het bedankt voor alle reacties.

<?php
$url = 'http://nl.wikipedia.org/w/api.php?action=parse&page=Nederland&format=json&prop=text&section=0';
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, "TestScript"); // required by wikipedia.org server; use YOUR user agent with YOUR contact information. (otherwise your IP might get blocked)
$c = curl_exec($ch);

$json = json_decode($c);

$content = $json->{'parse'}->{'text'}->{'*'}; // get the main text content of the query (it's parsed HTML)

// pattern for first match of a paragraph
$pattern = '#<p>(.*)</p>#Us'; // http://www.phpbuilder.com/board/showthread.php?t=10352690
if(preg_match($pattern, $content, $matches))
{
    // print $matches[0]; // content of the first paragraph (including wrapping <p> tag)
    print strip_tags($matches[1]); // Content of the first paragraph without the HTML tags.
}
?>
Okee nu wel even een passende classnaam bedenken. Leuk om te lezen dat het je gelukt is Patrick.

Reageren