PHP - 从 RSS feed 获取图像 url

发布于 2024-12-09 15:13:05 字数 1640 浏览 1 评论 0原文

我必须获取从 PHP 中的 rss feed 获取的图像的 url(不是链接)。我知道,如果我想获取其中一个提要标题的链接(不是 url),我必须使用 $feed->channel->item->link; 但是如果我想要获取 feed 项目的图像 url(不是链接)并将其插入到变量中以供以后使用?也许$feed->channel->image->url; - 我用过这个但没有任何作用。

这就是我用来获取饲料的。

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 . '"/>';
}}

提要是 http://obsrv.com/General/ImageFeed.aspx? (这里是带有图像搜索关键字的变量)我想从 rss feed 列表中获取第一个项目(第一个图像,因为每个 rss 帖子只有一个图像,并且还有一个此图像作为附加到帖子的文件如果对你有帮助的话就帮助我)。所以我想获取这个图像并将 url 取出到一个变量中,这样我就可以制作一个用于发布图像的 html 代码。

echo '<img title="' . $imagetitle . '" alt="' . $imagealt . '" src="' . $imagelink . '"/>';

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 . '"/>';

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心是晴朗的。 2024-12-16 15:13:05

您的代码中存在几个问题:

  1. 来自帮助页面,我可以看到您提供了错误的 URL 结构:

    “http://obsrv.com/General/ImageFeed.aspx?'$image'”
    

    应该是:

    "http://obsrv.com/General/ImageFeed.aspx?q=" 。 $图像。 ””
    

  2. 我认为您的代码中有错误:

    首先,将 $image 设置为 get_google_image($image) 的函数参数
    然后你设置从 wordpress 的函数中获取的 $image 。我不知道这是否是您想要的方法。

  3. 这段代码也有错误:

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

    我认为这就是您要找的:

    $image = urlencode($image);
    
  4. 另一个问题是,您提供了simplexml_load_file() 和 URL。

    更新:Phil 指出,如果启用了 fopen HTTP 包装器,simplexml_load_file() 确实可以使用 URL。但是,如果它被禁用,您可以使用此代码作为解决方法:

    $raw_html = file_get_contents( "http://obsrv.com/General/ImageFeed.aspx?q=$image" );
    $feed=simplexml_load_string( $raw_html );
    

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 );
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文