PHP 代码无法正常工作
这段代码有什么问题?
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
echo
$details .= '..';
$details 之前未定义,因此使用echo $details = '...'
echo
$details .= '..';
$details is not defined before so useecho $details = '...'
最后一行是问题所在:您将每个条目添加到 $details 中并另外回显它。只需将其
放在循环后面并删除循环中的回声即可。使用当前代码,您可以通过将内容添加到变量并回显来重复内容。
the last line is the problem: you add every entry to $details and echo it additionally. Just put
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.