PHP 代码无法正常工作

发布于 2024-10-24 19:15:30 字数 1249 浏览 2 评论 0原文

这段代码有什么问题?

$feedURL = 'http://gdata.youtube.com/feeds/api/standardfeeds/US/most_viewed?v=2&time=all_time';
$site_url = 'http://localhost/';    
// read feed into SimpleXML object
$sxml = simplexml_load_file($feedURL);
// 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 video player URL
$attrs = $media->group->player->attributes();
$watch = $attrs['url'];  
$video_id = str_replace('http://www.youtube.com/watch?v=', '',$watch);
$video_id = str_replace('&feature=youtube_gdata', '',$video_id);
$video_id = str_replace('_player', '',$video_id);
echo $details .= '
'.$site_url.'video/'.$video_id.'';}

该代码应返回 25 个 url 的列表(通过 youtube api feed),格式如下:

http://localhost/ video/video-id/

但是代码返回了一个丑陋的混合和重复的网址列表(具有我想要的格式)。

PS:在 str_replace 之前,链接显示正确,但格式为:

http:// /www.youtube.com/watch?v=video-id&feature=youtube_gdata_player

出了什么问题?我该如何解决这个问题?

What is the problem with this code?

$feedURL = 'http://gdata.youtube.com/feeds/api/standardfeeds/US/most_viewed?v=2&time=all_time';
$site_url = 'http://localhost/';    
// read feed into SimpleXML object
$sxml = simplexml_load_file($feedURL);
// 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 video player URL
$attrs = $media->group->player->attributes();
$watch = $attrs['url'];  
$video_id = str_replace('http://www.youtube.com/watch?v=', '',$watch);
$video_id = str_replace('&feature=youtube_gdata', '',$video_id);
$video_id = str_replace('_player', '',$video_id);
echo $details .= '
'.$site_url.'video/'.$video_id.'';}

The code should return a list of 25 url's (via the youtube api feed) with the format:

http://localhost/video/video-id/

But the code returns an ugly list of url's (whith the format i want) mixed and repeated.

PS: before str_replace the links were displayed correctly but with the format:

http://www.youtube.com/watch?v=video-id&feature=youtube_gdata_player

What's wrong? How can i resolve this?

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

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

发布评论

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

评论(2

临风闻羌笛 2024-10-31 19:15:30

echo $details .= '..'; $details 之前未定义,因此使用 echo $details = '...'

echo $details .= '..'; $details is not defined before so use echo $details = '...'

久随 2024-10-31 19:15:30

最后一行是问题所在:您将每个条目添加到 $details 中并另外回显它。只需将其

echo $details;

放在循环后面并删除循环中的回声即可。使用当前代码,您可以通过将内容添加到变量并回显来重复内容。

the last line is the problem: you add every entry to $details and echo it additionally. Just put

echo $details;

behind your loop and remove the echo in the loop, that's all. With your current code, you repeat the content by adding it to the variable AND echoing it.

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