Reputation:
I have to get the url (not the link) of an image that I'm getting from an rss feed in PHP. I know that if I want to get the link (not url) of one of the feed titles I have to use $feed->channel->item->link;
but what if I want to get the image url (not the link) of the feed item and insert it into a variable for later use? maybe $feed->channel->image->url;
- i used this but nothing works.
This is what I used to get the feed.
function get_google_image($image){
{ $intento=0;
$imagealt=get_the_title(); //its wordpress
$image=get_the_title();
$words = explode(" ", $image);
$imagetitle = implode(" ", array_splice($words, 0, 2));
$image = implode(" ", array_splice($words, 0, 2));
$image=str_replace(" ","+",$image);
while (!$feed=simplexml_load_file("http://obsrv.com/General/ImageFeed.aspx?'$image'")){
$attempt+=1;
if ($attempt>5) break;
print "Retry".$attempt."<br>";
}
$imagelink=$feed->channel->image->url; //Here is the problem, this variable finally gets empty and i want it to be the url of the image so i can post it with html later in the line below
echo '<img title="' . $imagetitle . '" alt="' . $imagealt . '" src="' . $imagelink . '"/>';
}}
The feed is http://obsrv.com/General/ImageFeed.aspx? (here the variable with the keyword for the image search) I want to get the first item from the rss feed list (the first image because every rss post has a only one image and also has a this image as an file attached to the post if it that helps you to help me). So I want to get this image and take out the url into a variable so then I can make a html code for posting an image.
echo '<img title="' . $imagetitle . '" alt="' . $imagealt . '" src="' . $imagelink . '"/>';
Upvotes: 0
Views: 2276
Reputation: 21979
You several issue in your code:
From the help page, I can see that you supplied the wrong URL structure:
"http://obsrv.com/General/ImageFeed.aspx?'$image'"
It should be:
"http://obsrv.com/General/ImageFeed.aspx?q=" . $image . ""
I think there's bug in your code:
First, you set $image
as function parameter to get_google_image($image)
And then you set $image
taken from wordpress's function. I don't know if this is your intended method.
This code is also buggy:
$image=str_replace(" ","+",$image);
I think this is what you looking for:
$image = urlencode($image);
Another issue is, you supplied simplexml_load_file()
with URL.
UPDATE: Phil pointed out that simplexml_load_file()
does work with URL, if fopen HTTP wrapper is enabled. But, if it's disable, you can use this code as a workaround:
$raw_html = file_get_contents( "http://obsrv.com/General/ImageFeed.aspx?q=$image" );
$feed=simplexml_load_string( $raw_html );
Upvotes: 1