Error in Getting Youtube Video Title, Description and thumbnail

為{幸葍}努か 提交于 2019-12-23 03:47:15

问题


I was getting youtube title and youtube description form the same code but now its not working I am getting following error:

Warning: DOMDocument::load() [domdocument.load]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /home/colorsfo/public_html/zaroorat/admin/pages/addSongProcess.php on line 16

Warning: DOMDocument::load(http://gdata.youtube.com/feeds/api/videos/Y7G-tYRzwYY) [domdocument.load]: failed to open stream: no suitable wrapper could be found in /home/colorsfo/public_html/zaroorat/admin/pages/addSongProcess.php on line 16

Warning: DOMDocument::load() [domdocument.load]: I/O warning : failed to load external entity "http://gdata.youtube.com/feeds/api/videos/Y7G-tYRzwYY" in /home/colorsfo/public_html/zaroorat/admin/pages/addSongProcess.php on line 16

.................................... Following Coding is used to get Youtube Video Data:

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

It was working before (This coding is working fine in Local server but on internet its not working) but now its not working. Please guide me how to fix this error. Thanks for your time.


回答1:


Your server's allow_url_fopen is disabled (so is mine). I feel your pain. Here's what I did.

Try using cURL, but return your data in json, using YouTube's v2 api. You do that by appending that data to the end of your url.

?v=2&alt=json

You didn't post how you're getting your YouTube ID, and that may be a part of the issue (though your sample url did work). So just in case, I'm also posting a simple function to retrieve the ID from the YouTube video url.

function get_youtube_id($url) {
    $newurl = parse_url($url);
    return substr($newurl['query'],2);
}

OK, now assuming you have your video id, you can run the following function for each field you wish to return.

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



回答2:


DOMDocuments' load() function uses PHPs fopen wrappers to retrieve files. It seems that on your webserver, allow_url_fopen is set to 0, thus disabling these wrappers. Try adding the following line to the top of your script:

ini_set ('allow_url_fopen', 1);

UPDATE: Try this:

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



回答3:


I hope it is not too late. My solution is to edit /etc/resolv.conf in your Linux machine: and replace first line with below line:

nameserver 8.8.8.8

Then save the file. no need for service restart.

Might work for servers who disabled some function accidentally for security.



来源:https://stackoverflow.com/questions/10551719/error-in-getting-youtube-video-title-description-and-thumbnail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!