Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get YouTube video title with video ID in PHP

I have to get the YouTube video title with the 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 an 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

The 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 an 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

The last method I tried was 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 it doesn't work either. The error reporting is E_ALL and display_errors is set to 1.

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

How can I get the video title with the video ID in PHP? Also an easy-to-follow tutorial for the YouTube API.

Note: This is not duplicate question.

Other answers on Stack Overflow about the YouTube API are pre 2013, and they no longer work.

like image 433
Yogie Avatar asked Oct 13 '15 19:10

Yogie


1 Answers

I got it working with file_get_contents(), but I used a different URL this time.

The URL I used was:

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

For example:

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');
like image 88
Yogie Avatar answered Oct 06 '22 00:10

Yogie