Yogie Yogie - 7 months ago 85
PHP Question

Get Youtube video title with video ID in PHP

First things first, THIS IS NOT DUPLICATE QUESTION
Other answers on StackOverflow about YouTube API are pre 2013, and no longer work.

I have to get the YouTube video title with YouTube video ID in PHP.

Currently I am doing it like this:

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


But this is throwing error as:

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


Above method works fine on Godaddy hosting, but not on 101domain.com hosting.

Another method I have tried is this:

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


This throws error as:

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


Last method I tried is this:

$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);


This doesn't throw any error, but doesn't work either.
Error reporting is
E_ALL
and
display_errors
is set to 1.

Somebody has told me it's safer to use YouTube API for this, since I am new to YouTube API, I need some help.

Can you please show how can I get the video title with video ID in PHP?
Also an easy to follow tutorials for YouTube API.

Answer

Use the Youtube API v3 and the list method sending in id as parameter and you will get info about that specific video https://developers.google.com/youtube/v3/docs/videos/list#id

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

With the id parameter

string The id parameter specifies a comma-separated list of the YouTube video ID(s) for the resource(s) that are being retrieved. In a video resource, the id property specifies the video's ID.

In your case with the php sdk the example would be something like

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