gpt4 book ai didi

php - 获取Youtube视频标题,说明和缩略图时出错

转载 作者:行者123 更新时间:2023-12-03 05:28:21 25 4
gpt4 key购买 nike

我正在以相同的代码获取youtube标题和youtube描述,但现在无法正常工作
我收到以下错误:

警告:DOMDocument::load()[domdocument.load]:在服务器配置中,第16行的/home/colorsfo/public_html/zaroorat/admin/pages/addSongProcess.php中的allow_url_fopen = 0禁用了http://包装器

警告:DOMDocument::load(http://gdata.youtube.com/feeds/api/videos/Y7G-tYRzwYY)[domdocument.load]:无法打开流:在/ home / colorsfo /中找不到合适的包装器第16行的public_html / zaroorat / admin / pages / addSongProcess.php

警告:DOMDocument::load()[domdocument.load]:I / O警告:无法在/ home / colorsfo中加载外部实体“http://gdata.youtube.com/feeds/api/videos/Y7G-tYRzwYY”第16行的/public_html/zaroorat/admin/pages/addSongProcess.php

....................................
以下编码用于获取Youtube视频数据:
$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;

以前曾经工作过(此编码在本地服务器上工作正常,但在Internet上不工作),但现在不工作。请指导我如何解决此错误。
谢谢你的时间。

最佳答案

您的服务器的allow_url_fopen已禁用(我的也是)。我感到你很痛苦。这就是我所做的。

尝试使用cURL,但使用YouTube的v2 API以json格式返回您的数据。您可以通过将数据附加到网址末尾来实现。

?v=2&alt=json

您没有发布获取YouTube ID的方式,但这可能是问题的一部分(尽管您的示例网址确实有用)。因此,以防万一,我还要发布一个简单的函数,以从YouTube视频网址中检索ID。
function get_youtube_id($url) {
$newurl = parse_url($url);
return substr($newurl['query'],2);
}

好的,现在假设您拥有自己的视频ID,则可以为要返回的每个字段运行以下功能。
// 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>";

关于php - 获取Youtube视频标题,说明和缩略图时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10551719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com