dreamice2013 2015-04-06 23:34
浏览 14
已采纳

YouTube API - 使用PHP和xml用户Feed来检索上次上传的视频

so I was trying to get last 5 YouTube user uploads through feed, and I ended with this code found online:

function yt_last_5() {
for($i = 0; $i < 5; ){
        error_reporting(E_ALL);
        $feedURL = 'http://gdata.youtube.com/feeds/api/users/' . yt_user_id(). '/uploads?max-results=5';
        $sxml = simplexml_load_file($feedURL);
        foreach ($sxml->entry as $entry) {
                $media = $entry->children('media', true);
                $url = (string)$media->group->player->attributes()->url;
                $index = strrpos($url, "&");
                $url = substr($url, 0, $index);
                $index = strrpos($url, "watch");
                $url = substr($url, 0, $index) . "v/" . substr($url, $index + 8, strlen($url) - ($index + 8));
                echo '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="400" height="250" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="' . $url . '" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="400" height="250" src="' . $url . '" allowscriptaccess="always" allowfullscreen="true"></embed></object><br />';
                break;
        }
        $i++;
}
}

The problem is that it shows the last uploaded video 5 times, actually I want it to retrieve the last 5 videos instead of repeating a single one.

last word: Thanks a lot!

  • 写回答

1条回答 默认 最新

  • doukongpao0903 2015-04-07 12:24
    关注

    You have two loops inside each other.

    One counts from 0 to 5:

    for($i = 0; $i < 5; )
    // some code
    $i++; // this could just be in the for(), by the way
    

    Inside that, you have some code which does the same thing every time, ignoring the counter. That contains a loop which looks at each video in turn:

    foreach ($sxml->entry as $entry) {
    

    But before it has a chance to look at anything other than the first entry, you break out of the inner loop:

    break;
    

    You only need one loop or the other.

    Using the counter approach, you could use $i to reference a particular entry in the XML:

    $sxml = simplexml_load_file($feedURL);
    for($i = 0; $i < 5; $i++) {
        $entry = $sxml->entry[$i];
        // display a video
    }
    

    Beware that this will fail if there are ever less than 5 entries; you could fix that by testing isset($sxml->entry[$i]).

    Using the foreach loop, you could count how many videos you've echoed and break when you get to the 5th:

    $sxml = simplexml_load_file($feedURL);
    $i = 0;
    foreach ($sxml->entry as $entry) {
        $i++;
        // display a video
        if ( $i == 5 ) {
            break;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。