2012-05-11 69 views
1

我得到YouTube的標題和YouTube描述形成相同的代碼,但現在它不工作 我收到以下錯誤:錯誤中獲取YouTube視頻的標題,描述和縮略圖

警告:DOM文檔::負載() domdocument.load]:在服務器配置中通過allow_url_fopen = 0禁用了http:// wrapper,位於第16行的/home/colorsfo/public_html/zaroorat/admin/pages/addSongProcess.php中

Warning:DOMDocument :: load (http://gdata.youtube.com/feeds/api/videos/Y7G-tYRzwYY)[domdocument.load]:無法打開流:沒有合適的包裝可以在/ home/colorsfo/public_html/zaroorat/admin /第16行的pages/addSongProcess.php

警告:DOMDocument :: load()[domdocument.load]:I/O警告:無法加載外部實體「http://gdata.youtube.com/feeds/api/videos/Y7G-tYRzwYY」in第16行的/home/colorsfo/public_html/zaroorat/admin/pages/addSongProcess.php

........................... ......... 下列編碼用於獲取YouTube視頻數據:

$url = "http://gdata.youtube.com/feeds/api/videos/".$embedCodeParts2[0]; $doc = new DOMDocument; @$doc->load($url); $title = $doc->getElementsByTagName("title")->item(0)->nodeValue; $videoDescription = $doc->getElementsByTagName("description")->item(0)->nodeValue;

它以前的工作(該編碼是工作在本地服務器很好,但互聯網上它不工作),但現在它不起作用。請指導我如何解決這個錯誤。 謝謝你的時間。

+0

你好穆罕默德,有沒有解決你的問題? –

回答

0

DOMDocuments的load()函數使用PHP的fopen包裝來檢索文件。 看來,在您的網絡服務器上,allow_url_fopen設置爲0,因此禁用這些包裝。 嘗試添加以下行到腳本的頂部:

ini_set ('allow_url_fopen', 1); 

UPDATE:試試這個:

<?php 
$url = "http://gdata.youtube.com/feeds/api/videos/" . $embedCodeParts2[0]; 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

$file = curl_exec($ch); 

curl_close($ch); 

$doc = new DOMDocument; 
@$doc->loadHTML($file); 
$title = $doc->getElementsByTagName("title")->item(0)->nodeValue; 
$videoDescription = $doc->getElementsByTagName("description")->item(0)->nodeValue; 
+0

你好,感謝回覆。 我試過這行代碼,但它根本不起作用。 請檢查此鏈接存在錯誤 [鏈接] http://www.colorsfocus.com/zaroorat/pages/allSongs.php – zzzzz

+0

這可能意味着您的主機已完全禁用allow_url_fopen。嘗試使用cURL(http://php.net/manual/en/book.curl.php)來檢索文件,然後使用'DOMDocument-> loadHTML()'函數 – Jeroen

+0

我不確定如何使用它你可以用任何例子向我解釋一下嗎? 謝謝 – zzzzz

3

你的服務器了allow_url_fopen被禁用(所以是我的)。我感到你的痛苦。這就是我所做的。

嘗試使用cURL,但使用YouTube的v2 api在json中返回數據。您可以通過將這些數據附加到網址的末尾來完成此操作。

?v=2&alt=json 

你沒有張貼你如何讓您的YouTube ID,那可能是問題的一部分(雖然你的樣品網址沒有工作)。爲了以防萬一,我還發布了一個簡單的功能來從YouTube視頻網址中檢索ID。好吧,現在假設你有你的視頻ID,你可以爲你想要返回的每個字段運行以下函數。

// Grab JSON and format it into PHP arrays from YouTube. 
// Options defined in the switch. No option returns entire array 
// Example of what the returned JSON will look like, pretty, here: 
// http://gdata.youtube.com/feeds/api/videos/dQw4w9WgXcQ?v=2&alt=json&prettyprint=true 
function get_youtube_info ($vid, $info) { 
    $youtube = "http://gdata.youtube.com/feeds/api/videos/$vid?v=2&alt=json"; 
    $ch = curl_init($youtube); 
    curl_setopt($ch, CURLOPT_HEADER, false); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    $output = curl_exec($ch); 
    curl_close($ch); 

    //If $assoc = true doesn't work, try: 
    //$output = json_decode($output, true); 
    $output = json_decode($output, $assoc = true); 

    //Add the ['feed'] in if it exists. 
    if ($output['feed']) { 
     $path = &$output['feed']['entry']; 
    } else { 
     $path = &$output['entry']; 
    } 

    //set up a switch to return various data bits to return. 
    switch($info) { 
     case 'title': 
      $output = $path['title']['$t']; 
      break; 
     case 'description': 
      $output = $path['media$group']['media$description']['$t']; 
      break; 
     case 'author': 
      $output = $path['author'][0]['name']; 
      break; 
     case 'author_uri': 
      $output = $path['author'][0]['uri']; 
      break; 
     case 'thumbnail_small': 
      $output = $path['media$group']['media$thumbnail'][0]['url']; 
      break; 
     case 'thumbnail_medium': 
      $output = $path['media$group']['media$thumbnail'][2]['url']; 
      break; 
     case 'thumbnail_large': 
      $output = $path['media$group']['media$thumbnail'][3]['url']; 
      break; 
     default: 
      return $output; 
      break; 
    } 
    return $output; 
} 

$url = "http://www.youtube.com/watch?v=oHg5SJYRHA0"; 
$id = get_youtube_id($url); 

echo "<h3><a href=" . $url . ">" . get_youtube_info($id, 'title') . "</a></h3>"; //echoes the title 
echo "<p><a href=" . $url . "><img style='float:left;margin-right: 5px;' src=" . get_youtube_info($id, 'thumbnail_small') . " /></a>" . get_youtube_info($id, 'description') . "</p>"; //echoes the description 
echo "<br style='clear:both;' /><pre>"; 
echo print_r(get_youtube_info($id)); 
echo "</pre>"; 
+0

Rick Roll的額外積分? –

+1

是的,你可以得到額外的積分,我愛你 - 謝謝你! – JoeRocc

+0

@WillLanni,如果你看到這個,請你告訴我如何縮短描述而不是完整的描述?謝謝 – user2166538

0

我希望現在還不算太晚。我的解決辦法是在你的Linux機器編輯/etc/resolv.conf:和替換第一行下面一行:

域名服務器8.8.8.8

然後保存文件。不需要重新啓動服務。

可能適用於因安全原因意外禁用某些功能的服務器。

相關問題