我敢肯定这可能是一个简单的解决方案,但是我看不到我的错误。我正在对youtube进行API调用,以使用视频ID获得有关youtube视频的一些基本信息;具体来说,我想要的是(1)标题,(2)描述,(3)标签和(4)缩略图。
通过网络浏览器加载API网址时,我会看到所有数据。我不想在这个问题中粘贴整个答复,但是将以下网址粘贴到浏览器中,您将看到我看到的内容:http://gdata.youtube.com/feeds/api/videos/_83A00a5mG4
如果仔细观察,您会看到media:thumbnail,media:keywords,内容等。我想要的一切都在那里。现在麻烦了...
当我通过以下功能(从Vimeo api复制...)加载相同的URL时,缩略图和关键字根本不存在。
function curl_get($url) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
$return = curl_exec($curl);
curl_close($curl);
return $return;
}
// youtube_get_ID is defined elsewhere...
$request_url = "http://gdata.youtube.com/feeds/api/videos/" . youtube_get_ID($url);
$video_data = simplexml_load_string(curl_get($request_url));
这些功能的确为我提供了一些数据的响应,但是缺少关键字和缩略图。谁能告诉我为什么我的缩略图和关键字丢失了?感谢您的任何帮助!
最佳答案
这是这个上的link for documentation。
这是我写的一个函数:
function get_youtube_videos($max_number, $user_name) {
$xml = simplexml_load_file('http://gdata.youtube.com/feeds/api/users/' . $user_name . '/uploads?max-results=' . $max_number);
$server_time = $xml->updated;
$return = array();
foreach ($xml->entry as $video) {
$vid = array();
$vid['id'] = substr($video->id,42);
$vid['title'] = $video->title;
$vid['date'] = $video->published;
//$vid['desc'] = $video->content;
// get nodes in media: namespace for media information
$media = $video->children('http://search.yahoo.com/mrss/');
// get the video length
$yt = $media->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->duration->attributes();
$vid['length'] = $attrs['seconds'];
// get video thumbnail
$attrs = $media->group->thumbnail[0]->attributes();
$vid['thumb'] = $attrs['url'];
// get <yt:stats> node for viewer statistics
$yt = $video->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->statistics->attributes();
$vid['views'] = $attrs['viewCount'];
array_push($return, $vid);
}
return $return;
}
这是实现:
$max_videos = 9;
$videos = get_youtube_videos($max_videos, 'thelonelyisland');
foreach($videos as $video) {
echo $video['title'] . '<br/>';
echo $video['id'] . '<br/>';
echo $video['date'] . '<br/>';
echo $video['views'] . '<br/>';
echo $video['thumb'] . '<br/>';
echo $video['length'] . '<br/>';
echo '<hr/>';
}
关于api - YouTube API:通过PHP中的curl丢失数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7591700/