PHP - 从 RSS feed 获取图像 url
我必须获取从 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码中存在几个问题:
来自帮助页面,我可以看到您提供了错误的 URL 结构:
应该是:
我认为您的代码中有错误:
首先,将
$image
设置为get_google_image($image)
的函数参数然后你设置从 wordpress 的函数中获取的
$image
。我不知道这是否是您想要的方法。这段代码也有错误:
我认为这就是您要找的:
另一个问题是,您提供了
simplexml_load_file() 和 URL。
更新:Phil 指出,如果启用了 fopen HTTP 包装器,
simplexml_load_file()
确实可以使用 URL。但是,如果它被禁用,您可以使用此代码作为解决方法:You several issue in your code:
From the help page, I can see that you supplied the wrong URL structure:
It should be:
I think there's bug in your code:
First, you set
$image
as function parameter toget_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:
I think this is what you looking for:
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: