2012-03-12 20 views
2

在我的申請,我節省了YouTube視頻的ID的一個YouTube視頻標題...喜歡「A4fR3sDprOE」 ..我有,以顯示其在應用程序標題。我得到了下面的代碼來獲得它的標題,並且它的工作正常。獲取PHP

現在的問題是,如果任何錯誤發生(在刪除的視頻的情況下)示出錯誤的php。我必須添加一個條件。但仍然顯示錯誤。

foreach($videos as $video) { 

    $video_id = $video->videos; 
    if($content=file_get_contents("http://youtube.com/get_video_info?video_id=".$video_id)) { 
     parse_str($content, $ytarr); 

     $myvideos[$i]['video_title']=$ytarr['title']; 

    } 
    else 
     $myvideos[$i]['video_title']="No title"; 

    $i++; 


} 

return $myvideos; 

在錯誤的情況下它的垂死與folllowing

嚴重性:警告

消息:的file_get_contents(http://youtube.com/get_video_info?video_id=A4fR3sDprOE)[function.file-獲取內容]:未能打開流:HTTP請求失敗! HTTP/1.0 402需要付費

文件名:型號/ webs.php

行號:128

請幫

回答

2

它的工作原理使用的file_get_contents前@(php-manual)?

類似:

if($content = @file_get_contents("http://youtube.com/get_video_info?video_id=".$video_id)) 

應該刪除錯誤,並用它來返回false的if語句

否則你可以使用try/catch語句(php-manual

try{ 
    // code 
}catch (Exception $e){ 
    // else code 
} 
+0

奏效...感謝.. – ramesh 2012-03-12 09:48:56

1

我相信你的託管服務提供商禁止用於安全目的的file_get_contents。你應該使用CURL。

<?php 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://youtube.com/get_video_info?video_id=".$video_id); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$response = curl_exec($ch); 
curl_close($ch); 
/* PARSE YOUTUBE'S RESPONSE AS YOU LIKE BELOW */ 
?> 
1

嘗試:

 

//use @ to supress warnings 
[email protected]_get_contents("http://youtube.com/get_video_info?video_id=".$video_id); 
if($content===FALSE) { 
    ..handle error here 
} 
else{ 
    ..rest of your code 
} 
 
11

這不是安全使用file_get_contents()與遠程URL。使用捲曲,而不是與YouTube API 2.0:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds/api/videos/'.$video_id); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

$response = curl_exec($ch); 
curl_close($ch); 

if ($response) { 
    $xml = new SimpleXMLElement($response); 
    $title = (string) $xml->title; 
} else { 
    // Error handling. 
} 
+1

的最佳解決方案,謝謝分享吧! – SeinopSys 2014-07-17 14:35:02

1

我想補充一點,在這裏看到的具體錯誤(HTTP 402 - 付款必須的,這是否則HTTP狀態代碼,服務器通常不具備實施)是當它已確定流量的「過度」的金額已經從您的IP地址(或IP地址範圍來向YouTube返回 - 他們寧願享受阻止OVH的IP範圍,頻繁的基礎[1][2])上。

因此,如果您正在訪問youtube.com(不是API)程序(使用PHP或其他),你最終可能會發現自己達不到這個錯誤。 YouTube一般首先將CAPTCHA提供給來自「過量流量」IP的請求,但如果您沒有完成它們(您的PHP腳本不會),他們將切換到這些毫無幫助的402響應,而且基本上無追索權 - YouTube不會「T恰好有一個客戶支持部門打電話,如果你不能實際訪問,因爲IP阻止任何他們的網站,你更沒有與他們聯繫的機會。

其他參考資料:
http://productforums.google.com/forum/#!topic/youtube/tR4WkNBPnUo http://productforums.google.com/forum/?fromgroups=#!topic/youtube/179aboankVg

6

這是我的解決方案。 It's很短。

$id = "VIDEO ID"; 
$videoTitle = file_get_contents("http://gdata.youtube.com/feeds/api/videos/${id}?v=2&fields=title"); 

preg_match("/<title>(.+?)<\/title>/is", $videoTitle, $titleOfVideo); 
$videoTitle = $titleOfVideo[1]; 
0

我的解決辦法是:

$xmlInfoVideo = simplexml_load_file("http://gdata.youtube.com/feeds/api/videos/".$videoId."?v=2&fields=title"); 

foreach($xmlInfoVideo->children() as $title) { $videoTitle = strtoupper((string) $title); } 

這讓視頻的標題。