I'm trying to set up a php page made up of images from 3 different RSS feeds, in 3 columns. The first feed is from a Wordpress blog, the second from an Etsy store, and the third from a Flickr feed.
What I would like to do is grab the link to the images from the first 3 items in each feed.
Relevant information:
The Wordpress feed puts the link in the description, which is formatted as CDATA.
The Etsy feed puts the image link as the first item in the description (E.g. <img src="http://ny-image2.etsy.com/il_155x125.126568958.jpg" />), but also has it in a <g:image_link> tag.
The Flickr feed has the link in the description, but it comes third after links for the user's profile and a link to the photo page. There is also <media:content url="http://farm5.static.flickr.com/4016/4377189674_d6f3aafa81_m.jpg"
type="image/jpeg"
height="159"
width="240"/>.
I've read over the basics of both php and simplexml but it seems what I'd like to do is too complicated for me to figure out on my own. I'd prefer to have a separate functions.php file so I only have to call the function on the web page.
ETA:
I've got the images for the Etsy feed pulled using
<?php
function getEtsyFeed($feed_url) {
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
echo "<ul>";
foreach($x->channel->item as $entry) {
echo "
<li>
<a href='$entry->link'><img src=" . $entry->children('g', true)->image_link . " /></a>
</li>";
}
echo "</ul>";
}
?>
and the images from the Flickr feed pulled using
<?php
function getFlickrFeed($feed_url) {
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
echo "<ul>";
foreach($x->channel->item as $entry) {
echo "
<li>
<a href='$entry->link'><img src=" . $entry->children('media', true)->thumbnail->attributes()->url . " /></a>
</li>";
}
echo "</ul>";
}
?>
I'm still not sure what to do about the blog feed, or how to only show the first 3 images without breaking the code that I do have.
You should try MagPie.
http://magpierss.sourceforge.net/
Related
I need to load some 3rd party widget onto my website. The only way they distribute it is by means of clumsy old <iframe>.
I don't have much choice so what I do is get an iframe html code, using a proxy page on my website like so:
$iframe = file_get_contents('http://example.com/page_with_iframe_html.php');
Then I have to remove some specific parts in iframe like this:
$iframe = preg_replace('~<div class="someclass">[\s\S]*<\/div>~ix', '', $iframe);
In this way I intend to remove the unwanted section. And in the end i simply output the iframe like so:
echo ($iframe);
The iframe gets output alright, however the unwanted section is still there. The regex itself was tested using regex101, but it doesn't work.
You should try this way, Hope this will help you out. Here i am using sample HTML remove the div with given class name, First i load the document, query and remove that node from the child.
Try this code snippet here
<?php
ini_set('display_errors', 1);
//sample HTML content
$string1='<html>'
. '<body>'
. '<div>This is div 1</div>'
. '<div class="someclass"> <span class="hot-line-text"> hotline: </span> <a id="hot-line-tel" class="hot-line-link" href="tel:0000" target="_parent"> <button class="hot-line-button"></button> <span class="hot-line-number">0000</span> </a> </div>'
. '</body>'
. '</html>';
$object= new DOMDocument();
$object->loadHTML($string1);
$xpathObj= new DOMXPath($object);
$result=$xpathObj->query('//div[#class="someclass"]');
foreach($result as $node)
{
$node->parentNode->removeChild($node);
}
echo $object->saveHTML();
I'm trying to do a PHP page to shows an album cover via Last.FM API. However, the Artist Name and the Title of the music are provided by a XML file that a software updates via FTP.
Here is the code of Last.FM api:
<?php
$img = simplexml_load_file('http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=<APIKEY>&artist=cher&track=believe');
echo '<img src="';
echo $img->track[0]->album[0]->image[3];
echo '">';
?>
Now the link of my XML file is: http://summerblast.pt/avaplayer/rds.xml
The info I need to the Last.FM API is in 'OnAir/CurMusic'.
Well, what I am trying to do is change "Cher" and "Believe" (the artist's name and the Title's name) in the link of the "simplexml_load_file" (in the php code) with the info that my XML file provides.
Can you please help me doing this?
Thank you all in advance.
Sorry was confused to what you were asking from before. I have gone ahead and corrected to do what you wanted.
You can get the Artist and the Title from rds.xml and just pass it to the URL like the following listed below (note it is important to run urlencode on them since they can have spaces and other things to break the URL):
Updated to do error checking
$xml = simplexml_load_file('http://summerblast.pt/avaplayer/rds.xml');
$artist = urlencode($xml->OnAir->CurMusic->Artist);
$track = urlencode($xml->OnAir->CurMusic->Title);
$url = 'http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=XXXXXXXXXXXXXXXXXXXXXXXXXXX&artist='.$artist.'&track='.$track;
$xml2 = #simplexml_load_file($url);
if ($xml2 === false)
{
echo("Url failed"); // do whatever you want to do
}
else
{
if($xml2->track->album->image[3])
{
echo '<img src="';
echo((string) $xml2->track->album->image[3]);
echo '">';
}
else
{
echo("artist does not have a image"); // do whatever you want to do
}
}
The script I am using to pull in the feed titles is:
<?php
function getFeed($feed_url) {
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
echo "<ul>";
foreach($x->channel->item as $entry) {
echo "<li><a href='$entry->link' title='$entry->title'>" . $entry->title . "</a> </li>";
$i++;
if($i==5) break;
}
echo "</ul>";
}
?>
I would like to pull in the images for each title/article and place them next to the title.
The float part is easy. I'm having trouble getting the actual image in. I've tried that mark like this: <img src="$entry->image" />, but it didn't work.
How would I go about this please?
As the answer to random's suggestion:
I'm using $item['description'] to represent the description string.
Then match for img tag
Output image tag
Then remove the image tag from the original $content and output that, you're left with just the description text
<?php
$content = $item['description'];
preg_match('/(<img[^>]+>)/i', $content, $matches);
echo $matches[0];
echo str_replace($matches[0],"",$content);
?>
When displaying the feed contents, variables such as $entry->link and $entry->title work because they are valid, present and required elements in a standard RSS feed item.
To call $entry->image would require the source RSS to use and populate that optional element.
Some feeds may include that data, but most will not.
Alternatively, you can write your own function or method to scan the contents of the description element and find any suitable image to include as you need.
I have a challenge with a wordpress theme I'm developing, but I think what I'm after is really just a general php/JS solution, nothing Wordpress specific.
So I have the below code with pulls in the thumbnail and description for an array of images I have uploaded. What I'd like to do is when a user clicks on the link the description and caption associated with that image is displayed in a div elsewhere on the page.
My issue is that so far the only way I can think of to do that is to set a javascript variable within my php foreach statement, but the problem with that is that this overwrites the variable value each time (as I can't change the variable name) so by the end of it all I don't have a different JS variable for each image, I just have one with the details from the last image in the array.
I know AJAX might be one way forward, but I know pretty much nothing about it. If anyone can help point me in the right direction I'd really appreciate it.
Thanks
<?php
$gallery_images = get_custom_field('galleryImages:to_array');
foreach ($gallery_images as $galleryID) {
$description = $attachment->post_content; //get image description
$caption = $attachment->post_excerpt; //get image caption
?>
link
<?php
}
?>
<div id="targetDiv"></div>
I think you're going about this the wrong way, personally. Using AJAX to interact with a WordPress site seems like overkill for the simple ability of showing some peripheral information about an image.
What I would do is have WordPress spit out the image, along with the caption info when the page is initially downloaded, but hide the caption info and then use client-side JavaScript to show/hide it when it's needed.
<?php
$button_html = "";
$caption_html = "";
$gallery_images = get_custom_field('galleryImages:to_array');
$gallery_images_count = 0;
foreach ($gallery_images as $galleryID) {
$description = $attachment->post_content; //get image description
$caption = $attachment->post_excerpt; //get image caption
$button_html .= '<div id="caption-button-' . $gallery_images_count . '" class="show-caption-button">Show Caption</div>';
$caption_html .= '<div id="caption-' . $gallery_images_count . '" style="display:none;">' . $caption . '</div>';
$gallery_images_count++;
}
echo '<div id="buttonDiv">' . $button_html . '</div>';
echo '<div id="targetDiv">' . $caption_html . '</div>';
?>
Then the JavaScript/jQuery:
$('.show-caption-button').click(function(){
var caption_id = $(this).prop('id').substring(15);
$('#caption-'+caption_id).eq(0).toggle();
});
It's hard to test without setting up a WordPress myself, but essentially what we're doing is adding caption divs with numbered id's to a string variable in PHP as we're looping through our images. Then, at the end of the loop, we echo that out to the page.
In JavaScript/jQuery, we're grabbing the corresponding id of the caption button and using it to toggle the relevant caption further down in the page.
I've got a nice little web app working that brings down playlists and videos from YouTube using the API. However, YouTube seems to fire back the playlists in the wrong order.
Any idea how I can do that?
Here is my very simple PHP code that does everything I need it to do (apart from date ordering)
<?php
// set feed URL
$feedURL = 'http://gdata.youtube.com/feeds/api/users/popcorncomedy/playlists?v=2';
// read feed into SimpleXML object
$sxml = simplexml_load_file($feedURL);
?>
<div class="container">
<h1><?php echo $sxml->title; ?></h1>
<?php
// iterate over entries in feed
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
// get <yt:duration> node for video length
$yt = $entry->children('http://gdata.youtube.com/schemas/2007');
?>
<a href="videos.php?playlist=<?php echo $yt->playlistId; ?>"><div class="item">
<span class="title"><?php echo $entry->title; ?></span><br /><br />
<span class="summary"><?php echo $entry->summary; ?></span>
</div></a>
<?php
} // closes the loop
?>
The XML element that contains the date is called: published in the date format: 2010-03-03T17:37:34.000Z
Any thoughts on how to achieve this simply?
I can't find a function within the API itself that orders content for you.
What you want is the orderby parameter http://code.google.com/apis/youtube/2.0/developers_guide_protocol_api_query_parameters.html#orderbysp however I haven't gotten it to work for playlists yet. As far as I can find its supports work with palylists but I never seen it work, hopefully you will have better luck!