Reham Fahmy
Reham Fahmy

Reputation: 5063

How to parse image feed with php

Using Wikipedia API link to get main image about some world known characters/events.

Example : (Stanislao Mattei)

This would show as following Wikipedia API Stanislao Mattei

Now my question I'd like to parse the xml to get image url to be shown up

here is the code i'm willing to use if it right ~ thanks to ccKep ~

<?PHP
ini_set("user_agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");

$url = "http://en.wikipedia.org/w/api.php?action=query&list=allimages&aiprop=url&format=xml&ailimit=1&aifrom=Stanislao Mattei";

$xml = simplexml_load_file($url);

$extracts = $xml->xpath("/api/query/allimages");

var_dump($extracts);
?>

It should gives results as array

how i can get among it the exact url of the image to be shown should be :

http://upload.wikimedia.org/wikipedia/en/a/a1/Stanislaus.jpg

to put it in html code

<img src="http://upload.wikimedia.org/wikipedia/en/a/a1/Stanislaus.jpg">

~ Thanks a lot

Upvotes: 1

Views: 1583

Answers (1)

Karo
Karo

Reputation: 744

Did you try $xml->query->allimages->img->attributes()->url


Your code will look like this:

<?php
ini_set("user_agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");

$url = "http://en.wikipedia.org/w/api.php?action=query&list=allimages&aiprop=url&format=xml&ailimit=1&aifrom=Stanislao Mattei";

$xml = simplexml_load_file($url);

$url = $xml->query->allimages->img->attributes()->url;

echo "URL: ".$url . "<br/>";
echo '<img src="'.$url.'">';

?>

Upvotes: 2

Related Questions