1 year ago (2016-06-21 16:45:10)

Snippet: Get Wikipedia Article Summery with PHP, JSON and Wiki API

7 0
174

Hello you, maybe i know you or don't but for sure would like to. But now i know that you more like to learn about JSON. 
Maybe you are friendly with JSON, that will help you a lot to understand the snippets but if your not, there is no broblem still. Just use the code as instructed. 
Now, i've created a small function here: 
  
function wiki($title){ 
$url = 'http://en.wikipedia.org/w/api.php?action=parse&page='.$keyword.'&format=json&prop=text&section=0'; 
$ch = curl_init($url); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_USERAGENT, 'TEST'); //add your CURL address(instead of TEST) because wikipedia recommends to use real init user instead of test to save your IP from blockage 
$c = curl_exec($ch); 
$json = json_decode($c); 
if($json !=''){ 
    $content = $json->{'parse'}->{'text'}->{'*'}; 
    $pattern = '#

(.*)

#Us'; 
        if(preg_match($pattern, $content, $matches)){ 
            if($matches[1] != ''){ 
                $con = preg_replace("/[[^)]+]/", '', $matches[1]); 
                $wiki = '

'.strip_tags($con).'

'; 
            } 
        } 

return $wiki; 
}
echo wiki(india);

Sign up or Sign in for free.
And start a new thread.
No Threads
More notes by Ghazi Khan