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

7 0 343 reads

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 = ''.$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 = '#


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



return $wiki; 
echo wiki(india);


Ghazi Khan /u/mgks

verified publisher79 feeds
No Threads
more feeds from /u/mgks