2013-02-16 71 views
0

我不知道什麼時候,但在某些時候,我的YouTube搜索方法停止工作,它已經工作了多年,但兩者的API和顯示嵌入視頻的首選方法發生了變化。我看谷歌官方文檔和我在谷歌API的開發版本3使用Zend或當前的選項,這些都是很多大的代碼庫比我現在用。搜索YouTube並以PHP顯示第一個視頻,需要建議?

我已經試過了代碼調試,最終可能得到它的工作,我應該只是廢了和更多的官方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; 
    } 
+0

一切看起來我用https://developers.google.com/youtube/youtube_player_demo建立HTML正確的,但雖然我找到一個視頻,不一定是視頻我想,現在不顯示在我的報價混搭什麼http://www.muschamp.ca/Muskie/favouriteQuotationsMashup.php?q=13 – Muskie 2013-02-16 21:33:44

+0

我從來沒有得到任何答覆,但我固定我的代碼,我仍然不知道如何使用正式的框架,但是開銷似乎沒有不證明功能,我想要做的就是搜索關鍵字詞組的頂級視頻,而我的代碼就是這樣做的。 http://blog.muschamp.ca/2013/02/17/searching-youtube-with-php/ – Muskie 2013-02-17 18:01:37

回答

0

我不知道爲什麼,因爲我沒有嘗試,但切換到的iFrame ,即這行代碼:現在

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

的作品,但我仍然不知道我是否應該跳火圈使用更正式的PHP框架之一,我真的只用這一個方法來訪問谷歌/ YouTube的API,這不是我的混搭的很大一部分,但我喜歡它的電影預告片和抒情歌的報價。