dongshaidu2456 2014-02-25 03:51
浏览 16
已采纳

视频ID以( - )减号时的PHP youtube xml Feed错误

I'm using this code to grab youtube videos information and it's working fine until I get an error when I try any video starts with (-) Minus Example: -VF0JwxQqcA

<?php
    //The Youtube"s API url
    define('YT_API_URL', 'http://gdata.youtube.com/feeds/api/videos?q=');
    //Change below the video id.
    $video_id2 = "$video_id2";
    //Using cURL php extension to make the request to youtube API
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, YT_API_URL . $video_id2);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //$feed holds a rss feed xml returned by youtube API
    $feed = curl_exec($ch);
    curl_close($ch);
    //Using SimpleXML to parse youtube"s feed
    $xml = simplexml_load_string($feed);
    $entry = $xml->entry[0];
    //If no entry whas found, then youtube didn"t find any video with specified id
    if(!$entry) exit('Error: no video with id "' . $video_id2 . '" whas found. Please specify the id of a existing video.');
    $media = $entry->children("media", true);
    $group = $media->group;

    $title = $group->title;//$title: The video title

    $desc = $group->description;//$desc: The video description
    $news_images = $group->thumbnail[0];//There are 4 thumbnails, the first one (index 0) is the largest.
    //$thumb_url: the url of the thumbnail. $thumb_width: thumbnail width in pixels.
    //$thumb_height: thumbnail height in pixels. $thumb_time: thumbnail time in the video
    list($thumb_url) = $news_images->attributes();
    $content_attributes = $group->content->attributes();
    //$vid_duration: the duration of the video in seconds. Ex.: 192.
    $vid_duration = $content_attributes["duration"];
    //$duration_formatted: the duration of the video formatted in "mm:ss". Ex.:01:54
    $source= "http://i.ytimg.com/vi/$video_id2/mqdefault.jpg";
?>

The error will be: Error: no video with id "-VF0JwxQqcA" whas found. Please specify the id of a existing video.

Any Help Please? Thanks

  • 写回答

1条回答 默认 最新

  • dqwyghl0649 2014-02-26 05:27
    关注

    The problem is that you're using the search feed, and so YouTube interprets your call with a video starting with a '-' as you intending to exclude that string. If you already know the video id, however, you shouldn't be using the search feed at all ... that's really intended to search keywords. If you know the videoID, you should use this feed:

    https://gdata.youtube.com/feeds/api/videos/-VF0JwxQqcA
    

    (in other words, get rid of the 'q' URL parameter, and then append the videoID as part of the URL path).

    You may need to change your parser slightly, but it's a more reliable way to return the video data.

    May I also suggest looking at moving to v3 of the API? You would use an endpoint like this:

    https://www.googleapis.com/youtube/v3/videos?part=id,snippet&id=-VF0JwxQqcA&key={YOUR_API_KEY}
    

    The data returned will be JSON, which is much easier to parse, and you won't have to worry about needing to change your app when v2 is deprecated. The info that v3 returns is also more complete for video resources.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用