0

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 . '"/>';
5
  • What's with the single-quotes around $image in the feed URL? Commented Oct 13, 2011 at 23:34
  • See this previous question: stackoverflow.com/questions/551246/…, it explains how to get attribute values from a simplexml node Commented Oct 13, 2011 at 23:35
  • Can you post the xml from that feed? It's not directly accessible on the url posted. Commented Oct 13, 2011 at 23:35
  • I thought the quotes are all wright, are they wrong? Commented Oct 13, 2011 at 23:43
  • this is an accessible feed url obsrv.com/General/ImageFeed.aspx?linux Commented Oct 14, 2011 at 0:11

1 Answer 1

1

You several issue in your code:

  1. 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 . ""
    
  2. 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.

  3. This code is also buggy:

    $image=str_replace(" ","+",$image);
    

    I think this is what you looking for:

    $image = urlencode($image);
    
  4. 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 );
    
Sign up to request clarification or add additional context in comments.

3 Comments

+1 Except for point #4. simplexml_load_file() can accept a URL argument if the fopen HTTP wrapper is enabled (which it usually is)
Ok, but that was not my question. When I use $image=str_replace(" ","+",$image); its because I want to replace any space in the tittle for a +. And I do not understand what you said about the $image parameter. Thanks...
point #1 is OBVIOUSLY related. Because, without it you can't even get the RSS. Maybe number 2 also, but that depending on what you really want to do with that code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.