Mostrando entradas con la etiqueta curl. Mostrar todas las entradas
Mostrando entradas con la etiqueta curl. Mostrar todas las entradas

martes, 29 de enero de 2013

Extraer información con API Wikipedia con PHP

En este código podremos ver como extraer la información desde la API de Wikipedia mediante PHP y Curl , concretamente exportaremos uno de los párrafos de un artículo concreto.

En la $url podemos ver que consta de varios parámetros, el parámetro page es la página de Albert Einstein, este dato lo obtenemos de la dirección del artículo que nos interesa:
es.wikipedia.org/wiki/Albert_Einstein


In this code we can see how to extract information from Wikipedia API using PHP and Curl, specifically will export one paragraph of a particular article.

In the $ url can see which consists of several parameters, the parameter page is the page of Albert Einstein, this data we get from the direction of the article we are interested in:
es.wikipedia.org / wiki / Albert_Einstein


<?
$url='http://es.wikipedia.org/w/api.php?action=parse&page=Albert_Einstein&format=json&prop=text&section=1&rvsection=2rvparse&rvprop=content';
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, "TestScript");
$c = curl_exec($ch);
$json = json_decode($c);
$content = $json->{'parse'}->{'text'}->{'*'};
$final = strip_tags($matches[0], "<p>, <br/>");
echo nl2br($final); 
?>

Otra Opción:
<?
$url = 'http://es.wikipedia.org/w/api.php?action=parse&page=Albert_Einstein&format=json&prop=text&section=1&rvsection=2rvparse&rvprop=content';
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, "TestScript");
$c = curl_exec($ch);
$json = json_decode($c);
$content = $json->{'parse'}->{'text'}->{'*'};
$pattern = '#<p>(.*)</p>#Us';
if(preg_match($pattern, $content, $matches))
{
print strip_tags($matches[1]);
}
?>



En este caso, nos mostrará el párrafo del nacimiento de Albert Einstein.