搜索YouTube并在PHP中显示第一个视频,需要建议吗?

时间:2013-02-16 21:26:41

标签: php youtube-api

我不确定何时,但在某些时候我的YouTube搜索方法停止工作,它已经工作多年,但API和显示嵌入式视频的首选方法已经改变。我查看了官方的Google文档,我可以选择使用Zend或目前正在开发的Google 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;
   }

1 个答案:

答案 0 :(得分:0)

我不确定原因,因为我确实尝试过,但切换到iFrame,即这行代码:

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

现在可以使用,但是我仍然想知道是否应该通过箍来使用一个更官方的PHP框架,我真的只使用这一种方法来访问Google / YouTube api,它不是很重要的一部分我的混搭,但我喜欢它的电影预告片和歌词抒情语录。