2015-10-13 117 views
0

第一件事,这不是重复的问题有关YouTube API在计算器上 其他的答案是预先2013年,并且不再工作。获取视频ID的Youtube视频标题在PHP第一

我必须使用PHP中的YouTube视频ID获取YouTube视频标题。

目前,我这样做是这样的:

$html = "http://www.youtube.com/watch?v=".$video_id; 
    $doc = new DOMDocument(); 
    $doc->loadHTMLFile($html); 
    $doc->preserveWhiteSpace = false; 
    $title_div = $doc->getElementById('eow-title'); 
    $title = $title_div->nodeValue; 

但这是抛出错误为:

Warning: DOMDocument::loadHTMLFile(http://www.youtube.com/watch?v=hMpCsfvi_3c): failed to open stream: HTTP request failed! HTTP/1.0 429 Too Many Requests in /home/vhosts/xxxx/xxxx/get_youtube_title.php on line 6 

Warning: DOMDocument::loadHTMLFile(): I/O warning : failed to load external entity "http://www.youtube.com/watch?v=hMpCsfvi_3c" in /home/vhosts/xxxx/xxxx/get_youtube_title.php on line 6 

以上方法适用于GoDaddy的托管罚款,但不能在101domain.com托管。

我已经尝试了另一种方法是这样的:

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"; 

这将引发错误为:

Warning: DOMDocument::loadHTMLFile(http://youtube.com/get_video_info?video_id=hMpCsfvi_3c): failed to open stream: HTTP request failed! HTTP/1.0 402 Payment Required in /home/vhosts/xxxx/get_youtube_title.php on line 6 

Warning: DOMDocument::loadHTMLFile(): I/O warning : failed to load external entity "http://youtube.com/get_video_info?video_id=hMpCsfvi_3c" in /home/vhosts/xxxx/get_youtube_title.php on line 6 

末方法我想是这样的:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://youtube.com/get_video_info? video_id=".$video_id); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$response = curl_exec($ch); 
curl_close($ch); 

这不扔任何错误,但不起作用。 错误报告是E_ALLdisplay_errors设置为1.

有人告诉我,使用YouTube API更安全,因为我是YouTube API的新手,我需要一些帮助。

你能告诉我怎样才能得到带有视频ID的视频标题在PHP中? 也是一个易于使用的YouTube API教程。

+1

答案 “易于遵循教程” 的一部分 - https://github.com/ madcoda/php-youtube-api/wiki /开始与php-composer :) – marmeladze

回答

1

使用YouTube API v3和列表方法ID发送作为参数,你会得到有关该特定视频https://developers.google.com/youtube/v3/docs/videos/list#id

信息GET https://www.googleapis.com/youtube/v3/videos

与ID参数

字符串 id参数指定一个或多个资源的YouTube视频ID的逗号分隔列表,重新被检索。在视频资源中,id属性指定视频的ID。

在你的情况与PHP SDK中的例子是像

# Call the videos.list method to retrieve location details for each video. 
$videosResponse = $youtube->videos->listVideos('snippet, recordingDetails', array(
'id' => $videoIds, 
)); 
+1

@Slowmove我以另一种方式工作,但您的答案帮助我达到了该解决方案。感谢您的努力。 :) 愿上帝保佑你!! – Yogie

3

这为我工作:

$video_id = ...; 
$url = "http://www.youtube.com/watch?v=".$video_id; 

$page = file_get_contents($url); 
$doc = new DOMDocument(); 
$doc->loadHTML($page); 

$title_div = $doc->getElementById('eow-title'); 
$title = $title_div->nodeValue; 

var_dump($title); 
exit; 

而且也是这个工作(这是你的第一个基本相同方法):

$url = "http://www.youtube.com/watch?v=".$video_id; 

$doc = new DOMDocument(); 
$doc->preserveWhiteSpace = FALSE; 
$doc->loadHTMLFile($url); 

$title_div = $doc->getElementById('eow-title'); 
$title = $title_div->nodeValue; 

var_dump($title); 
exit; 

所以我在想你问题与您的代码以外的其他内容相关。

3

我得到它与file_get_contents()一起工作,但我这次使用了不同的URL。

我使用的网址是:

https://www.googleapis.com/youtube/v3/videos?id='.$video_id.'&key='.$YoutubeAPIKey.'&part=snippet

例如:

function get_youtube_title($video_id){ 
    $html = 'https://www.googleapis.com/youtube/v3/videos?id='.$video_id.'&key=alskdfhwueoriwaksjdfnzxcvxzfserwesfasdfs&part=snippet'; 
    $response = file_get_contents($html); 
    $decoded = json_decode($response, true); 
    foreach ($decoded['items'] as $items) { 
     $title= $items['snippet']['title']; 
     return $title; 
    } 
} 
echo $title = get_youtube_title('PQqudiUdGuo'); 
0
function get_youtube($url){ 

    $youtube = "http://www.youtube.com/oembed?url=". $url ."&format=json"; 
    $curl = curl_init($youtube); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    $return = curl_exec($curl); 
    curl_close($curl); 
    return json_decode($return, true); 
} 

$url = // youtube video url 
// Display Data 
print_r(get_youtube($url));