Fetch description from wikipedia api of any keyword

Below is the code to fetch the description from wikipedia api of any keyword. Here we fetch the spices description.

$query='pongo abelii';
$url = 'https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles='.rawurlencode($query);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); 
$res = curl_exec($ch);
curl_close($ch);
$json = json_decode($res);
$data = $json->query->pages;
$page_id = '';
foreach($data as $key => $value){
    $page_id = $key;
}
echo $description = $data->$page_id->extract;

If you want to fetch description with limited characters you can use the code below.

$query='pongo abelii';
$url = 'https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles='.rawurlencode($query).'&exchars=175';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); 
$res = curl_exec($ch);
curl_close($ch);
$json = json_decode($res);
$data = $json->query->pages;
$page_id = '';
foreach($data as $key => $value){
    $page_id = $key;
}
echo $description = $data->$page_id->extract;