Youtube API:在PHP中通过curl丢失数据?

Youtube API:在PHP中通过curl丢失数据?,api,curl,youtube,thumbnails,Api,Curl,Youtube,Thumbnails,我确信这可能是一个简单的解决方案,但我看不出我的错误。我正在对youtube进行API调用,以使用视频的ID获取youtube视频的一些基本信息;具体来说,我想要的是(1)标题,(2)描述,(3)标签和(4)缩略图 当我通过web浏览器加载API url时,我会看到所有数据。我不想粘贴此问题中的整个回答,但请将以下url粘贴到浏览器中,您将看到我看到的内容: 如果你仔细看,你会看到媒体:缩略图、媒体:关键词、内容等等。我想要的一切都在那里。现在麻烦来了 当我通过以下函数(我从Vimeo api复

我确信这可能是一个简单的解决方案,但我看不出我的错误。我正在对youtube进行API调用,以使用视频的ID获取youtube视频的一些基本信息;具体来说,我想要的是(1)标题,(2)描述,(3)标签和(4)缩略图

当我通过web浏览器加载API url时,我会看到所有数据。我不想粘贴此问题中的整个回答,但请将以下url粘贴到浏览器中,您将看到我看到的内容:

如果你仔细看,你会看到媒体:缩略图、媒体:关键词、内容等等。我想要的一切都在那里。现在麻烦来了

当我通过以下函数(我从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));
这些函数确实给了我一些数据的响应,但是缺少关键字和缩略图。有人能告诉我为什么我的缩略图和关键字丢失了吗?谢谢你的帮助

这是关于这个问题的答案

这是我写的一个函数:

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;
}
函数获取youtube视频($max\u number,$user\u name){
$xml=simplexml\u加载\u文件('http://gdata.youtube.com/feeds/api/users/“.$user\u name./uploads?max results=”.$max\u number);
$server\u time=$xml->已更新;
$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;
//获取媒体中的节点:媒体信息的命名空间
$media=$video->children($media)http://search.yahoo.com/mrss/');
//获取视频长度
$yt=$media->children($yt)http://gdata.youtube.com/schemas/2007');
$attrs=$yt->duration->attributes();
$vid['length']=$attrs['seconds'];
//获取视频缩略图
$attrs=$media->group->缩略图[0]->attributes();
$vid['thumb']=$attrs['url'];
//获取查看器统计信息的节点
$yt=$video->children($yt)http://gdata.youtube.com/schemas/2007');
$attrs=$yt->statistics->attributes();
$vid['views']=$attrs['viewCount'];
阵列推送($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/>';
}
$max\u视频=9;
$videos=get_youtube_videos($max_videos,'TheLonely Island');
foreach($视频作为$视频){
echo$video['title']。
; echo$video['id']。
; echo$video['date']。
; echo$video['views']。
; echo$video['thumb']。
; echo$video['length']。
; 回声“
”; }
这是关于这个问题的答案

这是我写的一个函数:

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;
}
函数获取youtube视频($max\u number,$user\u name){
$xml=simplexml\u加载\u文件('http://gdata.youtube.com/feeds/api/users/“.$user\u name./uploads?max results=”.$max\u number);
$server\u time=$xml->已更新;
$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;
//获取媒体中的节点:媒体信息的命名空间
$media=$video->children($media)http://search.yahoo.com/mrss/');
//获取视频长度
$yt=$media->children($yt)http://gdata.youtube.com/schemas/2007');
$attrs=$yt->duration->attributes();
$vid['length']=$attrs['seconds'];
//获取视频缩略图
$attrs=$media->group->缩略图[0]->attributes();
$vid['thumb']=$attrs['url'];
//获取查看器统计信息的节点
$yt=$video->children($yt)http://gdata.youtube.com/schemas/2007');
$attrs=$yt->statistics->attributes();
$vid['views']=$attrs['viewCount'];
阵列推送($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/>';
}
$max\u视频=9;
$videos=get_youtube_videos($max_videos,'TheLonely Island');
foreach($视频作为$视频){
echo$video['title']。
; echo$video['id']。
; echo$video['date']。
; echo$video['views']。
; echo$video['thumb']。
; echo$video['length']。
; 回声“
”; }
如果youtube关闭,或者没有及时响应,会发生什么情况。这是失败的。curl是一个更好的选择,可以通过设置超时来检索信息。如果youtube关闭,或者没有及时响应,会发生什么。这是失败的。curl是一个更好的选择,可以通过设置超时来检索信息。