搜索YouTube并用PHP显示第一个视频,需要建议


Searching YouTube and displaying first video in PHP, advice needed?

我不确定是什么时候,但在某个时候,我的YouTube搜索方法停止了工作,它已经工作了多年,但API和显示嵌入式视频的首选方法都发生了变化。我看过了谷歌的官方文档,我可以选择使用Zend或目前正在开发的谷歌API版本3,这些代码库比我目前使用的代码库大得多。

我已经试过调试我的代码,如果我只是废弃它,并将更官方的PHP代码库集成到我的项目中,最终可能会让它重新工作。这就是我的方法所在的地方,它可以找到数据,但我似乎不显示任何视频。。。

public function embeddableVideoClipFor($searchString)
   {
        // Previous experience revealed that video search is not perfect, but we're just going to giver and create an embedded player with the 
        // top result or return NULL
        // I used this as a guide to build my embedded player http://code.google.com/apis/youtube/youtube_player_demo.html
        $embeddableVideoClipHTML = NULL;
        // Further details on searching YouTube http://www.ibm.com/developerworks/xml/library/x-youtubeapi/
        // This was working well for over two years but now I'm getting no videos, I may have been throttled or more likely the API has changed...
        // Before switching to Zend or going to switch to version 3.0 of Google/YouTube API I should try and debug...
        $vq = $searchString;
        $vq = preg_replace('/[[:space:]]+/', ' ', trim($vq));
        $vq = urlencode($vq);
        $feedURL = 'http://gdata.youtube.com/feeds/api/videos?q=' . $vq . '&safeSearch=none&orderby=viewCount&v=2'; // Added version two tag...

        print_r($feedURL);  

        // read feed into SimpleXML object
        try
        {
            $youTubeXML = simplexml_load_file($feedURL);
        }
        catch(Exception $e)
        {   
            // This rarely throws an error, but when it does, I just want to pretend I can't find a video clip
            $youTubeXML = NULL;
        }   
        print("<pre>");
        print_r($youTubeXML);
        print("</pre>");
        if(($youTubeXML != NULL) && ( ! empty($youTubeXML->entry->link[0]['href'])))
        {
            $videoLink = $youTubeXML->entry->link[0]['href'];  // This is not enough, I need to trim the beginning and end off this to just get the video code
            $trimedURL = str_replace('http://www.youtube.com/watch?v=', '' , $videoLink);
            $videoCode = str_replace('&feature=youtube_gdata', '', $trimedURL);
            // $embeddableVideoClipHTML = '<object style="height: 390px; width: 640px"><param name="movie" value="http://www.youtube.com/v/' . $videoCode . '"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><embed src="http://www.youtube.com/v/' . $videoCode . '?version=3" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="640" height="390"></object>';
            // $embeddableVideoClipHTML = '<iframe id="ytplayer" type="text/html" width="640" height="360" src="https://www.youtube.com/embed/' . $videoCode . '"frameborder="0" allowfullscreen>';
            // Version 3
            $embeddableVideoClipHTML = '<object width="640" height="360"><param name="movie" value="https://www.youtube.com/v/' . $videoCode . '?version=3"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed src="https://www.youtube.com/v/' . $videoCode . '?version=3" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="640" height="360"></embed></object>';
        }
        return $embeddableVideoClipHTML;
   }

我不知道为什么,因为我确实尝试过,但切换到了iFrame,即这行代码:

// $embeddableVideoClipHTML = '<iframe id="ytplayer" type="text/html" width="640" height="360" src="https://www.youtube.com/embed/' . $videoCode . '"frameborder="0" allowfullscreen>';

现在可以工作了,但我仍然想知道我是否应该跳过重重困难,使用一个更官方的PHP框架,我真的只使用这一种方法来访问Google/UTubeAPI,它不是我mashup的重要组成部分,但我喜欢它用于电影预告片和歌词引用。