Fetch images URL from XML - php

I am using the below code to fetch the $movie->id from the response XML
<?php
$movie_name='Dabangg 2';
$url ='http://api.themoviedb.org/2.1/Movie.search/en/xml/accd3ddbbae37c0315fb5c8e19b815a5/%22Dabangg%202%22';
$xml = simplexml_load_file($url);
$movies = $xml->movies->movie;
foreach ($movies as $movie){
$arrMovie_id = $movie->id;
}
?>
the response xml structure is
How to fetch image URL with thumb size?

See the below an easy way to get only specific images.
$xml = simplexml_load_file($url);
$images = $xml->xpath("//image");
//echo "<pre>";print_r($images);die;
foreach ($images as $image){
if($image['size'] == "thumb"){
echo "URL:".$image['url']."<br/>";
echo "SIZE:".$image['size']."<br/>";
echo "<hr/>";
}
}

Use the attributes() method of SimpleXmlElement.
Example:
$imageAttributes = $movie->images[0]->attributes();
$size = $imageAttributes['size'];
See the documentation at: http://www.php.net/manual/en/simplexmlelement.attributes.php

EDIT: select only URL attributes with size = "thumb" and type = "poster":
$urls = $xml->xpath("//image[#size='thumb' and #type='poster']/#url");
if you expect only 1 url, do:
$url = (string)$xml->xpath("//image[#size='thumb' and #type='poster']/#url")[0];
echo $url;
working live demo: http://codepad.viper-7.com/wdmEay

Related

Use 'foreach' in script

I'm trying to write a script that list all the image URL from an specific URL. I used foreach in order to scan several pages, but I think os not working well.
This is my code:
<?php
include('simple_html_dom.php');
$array = array($page, $page2);
$page = "https://www.dllo.dev";
$page2 = "https://www.dllo2.dev";
$html = new simple_html_dom();
$html->load_file($array);
$images = array();
foreach($html->find('img') as $element) {
$images[] = $element->src;
}
reset($images);
echo "URL $array:<br /><br />";
foreach ($images as $out) {
$url = "$base$out";
echo "$url,&nbsp";
}
It's partially working, but only with the first URL ($page)... Any idea?
:D

Printing thumbnails using simpleXML parse - how to return all?

I have looked for this answer to this question on here but I can't seem to find anything which is relevant to this particular issue.
I am currently using simpleXML to parse an RSS feed, in order to return thumbnail images by going through the nodes to parse "media:thumbnail". I have managed to do this and return all thumbnail URLs, so I know that I am getting to the right content, like so:
<?php
$url = "http://feeds.bbci.co.uk/news/rss.xml?edition=uk";
$xml = simplexml_load_file($url);
foreach($xml->channel->item as $item) {
$media = $item->children('media', 'http://search.yahoo.com/mrss/');
foreach($media->thumbnail as $thumb) {
echo $thumb->attributes()->url;
}
}
?>
This echos all the image urls. But when I store this in to a variable and try to echo this later as the img src, it only returns one image, rather than all:
<?php
$url = "http://feeds.bbci.co.uk/news/rss.xml?edition=uk";
$xml = simplexml_load_file($url);
foreach($xml->channel->item as $item) {
$media = $item->children('media', 'http://search.yahoo.com/mrss/');
foreach($media->thumbnail as $thumb) {
$image = $thumb->attributes()->url;
}
}
?>
<div><img src = <?php echo $image; ?> /></div>
How can I echo all of the URLs in to individual images? Thanks for looking.
Since you're getting and expecting multiple image urls, might as well store them inside an array:
$images_container = array();
foreach($xml->channel->item as $item) {
$media = $item->children('media', 'http://search.yahoo.com/mrss/');
foreach($media->thumbnail as $thumb) {
$image = $thumb->attributes()->url;
$images_container[] = (string) $image;
}
}
echo '<pre>', print_r($images_container, 1), '<pre>';
Sample Output
Now of course, if you want to process those array of string image urls, then just use and process the container:
<?php foreach($images_container as $url): ?>
<div><img src="<?php echo $url; ?>" alt="" /></div>
<?php endforeach; ?>
Pictures
Try xpath.
$url = "http://feeds.bbci.co.uk/news/rss.xml?edition=uk";
$xml = simplexml_load_file($url);
$xml->registerXPathNamespace( 'media', 'http://search.yahoo.com/mrss/' );
// get only thumbnails of specified width
$xpath = $xml->xpath( '//media:thumbnail[#url and #width=144]' );
/**
* The above xpath will get only thumbnails of width 144
*/
foreach( $xpath as $node ) {
echo '<div><img src="' . $node['url'] . '" /></div>';
}
Hope that helps.

PHP - Get random image from xml file

I want to display a random image from the Unsplash.com RSS feed (https://unsplash.com/rss) using PHP. I've read about how to this, but I can't seem to quite understand it. I think the easiest way to do this is would be with SimpleXML. Then I'd like to use the image as a background-image for my website. Can anyone help me out with this? Thanks!
Try this :
function getRandomImageFromRSS($url = 'https://unsplash.com/rss')
{
$xml = simplexml_load_file($url);
$images = array();
foreach ($xml->channel->item as $item)
{
foreach ($item->image->url as $url)
{
$images[] = (string) $url;
}
}
shuffle($images);
return isset($images[0]) ? $images[0] : null;
}
echo getRandomImageFromRSS();

How to turn variable feed to array?

I want to turn the variable into an array so I can store more than one feed?
<?php
error_reporting(0);
$feed_lifehacker_full = simplexml_load_file('http://feeds.gawker.com/lifehacker/full');
$xml = $feed_lifehacker_full;
//print_r($xml);
foreach ($xml->channel->item as $node){
$title = $node->title;
$link = $node->link;
$link = explode('/', $link);
$link = $link[8];
$url = $node->url;
$description = $node->description;
$pubDate = $node->pubDate;
preg_match_all('#(http://img[^\s]+(?=\.(jpe?g|png|gif)))#i', $description[0], $images);
$images = $images[0][1] . '.jpg';
if($images == '.jpg'){
//uncomment to show youtube articles
//$images = "http://placehold.it/640x360";
//echo "<a href='page2.php?a=$link' title='$title'><img src='$images' /></a><br>";
} else {
//article image
$images . '<br>';
echo "<a href='page2.php?a=$link' title='$title'><img src='$images' /></a><br>";
}
}
How can I change this to load to arrays,
$feed_lifehacker_full = simplexml_load_file('http://feeds.gawker.com/lifehacker/full');
$xml = $feed_lifehacker_full;
The script is just gathering the image of an rss feed and linking to a page, if you see how it can be done more efficiently feel free to say
it is possible to encode the result given as json and by decoding it it will return you an array
$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$array = json_decode($json, TRUE);

Select specific Tumblr XML values with PHP

My goal is to embed Tumblr posts into a website using their provided XML. The problem is that Tumblr saves 6 different sizes of each image you post. My code below will get the first image, but it happens to be too large. How can I select one of the smaller-sized photos out of the XML if all the photos have the same tag of <photo-url>?
→ This is the XML from my Tumblr that I'm using: Tumblr XML.
→ This is my PHP code so far:
<?php
$request_url = "http://kthornbloom.tumblr.com/api/read?type=photo";
$xml = simplexml_load_file($request_url);
$title = $xml->posts->post->{'photo-caption'};
$photo = $xml->posts->post->{'photo-url'};
echo '<h1>'.$title.'</h1>';
echo '<img src="'.$photo.'"/>"';
echo "…";
echo "</br><a target=frame2 href='".$link."'>Read More</a>";
?>
The function getPhoto takes an array of $photos and a $desiredWidth. It returns the photo whose max-width is (1) closest to and (2) less than or equal to $desiredWidth. You can adapt the function to fit your needs. The important things to note are:
$xml->posts->post->{'photo-url'} is an array.
$photo['max-width'] accesses the max-width attribute on the <photo> tag.
I used echo '<pre>'; print_r($xml->posts->post); echo '</pre>'; to find out $xml->posts->post->{'photo-url'} was an array.
I found the syntax for accessing attributes (e.g., $photo['max-width']) at the documentation for SimpleXMLElement.
function getPhoto($photos, $desiredWidth) {
$currentPhoto = NULL;
$currentDelta = PHP_INT_MAX;
foreach ($photos as $photo) {
$delta = abs($desiredWidth - $photo['max-width']);
if ($photo['max-width'] <= $desiredWidth && $delta < $currentDelta) {
$currentPhoto = $photo;
$currentDelta = $delta;
}
}
return $currentPhoto;
}
$request_url = "http://kthornbloom.tumblr.com/api/read?type=photo";
$xml = simplexml_load_file($request_url);
foreach ($xml->posts->post as $post) {
echo '<h1>'.$post->{'photo-caption'}.'</h1>';
echo '<img src="'.getPhoto($post->{'photo-url'}, 450).'"/>"';
echo "...";
echo "</br><a target=frame2 href='".$post['url']."'>Read More</a>";
}
To get the photo with max-width="100":
$xml = simplexml_load_file('tumblr.xml');
echo '<h1>'.$xml->posts->post->{'photo-caption'}.'</h1>';
foreach($xml->posts->post->{'photo-url'} as $url) {
if ($url->attributes() == '100')
echo '<img src="'.$url.'" />';
}
Maybe this:
$doc = simplexml_load_file(
'http://kthornbloom.tumblr.com/api/read?type=photo'
);
foreach ($doc->posts->post as $post) {
foreach ($post->{'photo-url'} as $photo_url) {
echo $photo_url;
echo "\n";
}
}

Categories