替换gdata

时间:2015-09-26 00:10:51

标签: php youtube-api

我有一个像这样的URL,它包含在一个php文件中,用于查找有关视频的统计信息。

由于gdata不再有效,我现在应该怎么做?我在API中找不到它!         ( 'http://gdata.youtube.com/schemas/2007'); //获取查看器统计信息的节点         http://gdata.youtube.com/feeds/api/videos/?q= 我不太清楚该怎么做

-OLD代码 - 这是我的问题所在。我需要弄清楚这是用新API编写的,

$ media = $ entry-> children('http://search.yahoo.com/mrss/'); //获取媒体中的节点:媒体信息的命名空间

$attrs = $media->group->player->attributes();              // get video player URL
$watch = $attrs['url']; 

$attrs = $media->group->thumbnail[0]->attributes();        // get video thumbnail
$thumbnail = $attrs['url'];

$yt = $media->children('http://gdata.youtube.com/schemas/2007');   // get <yt:duration> node for video length
$attrs = $yt->duration->attributes();
$length = $attrs['seconds']; 

$yt = $entry->children('http://gdata.youtube.com/schemas/2007');   // get <yt:stats> node for viewer statistics
$attrs = $yt->statistics->attributes();
$viewCount = $attrs['viewCount']; 

$gd = $entry->children('http://schemas.google.com/g/2005');        // get <gd:rating> node for video ratings
if ($gd->rating) {
    $attrs = $gd->rating->attributes();
    $rating = $attrs['average'];
}else{$rating = 0;}

$vid = preg_replace ('/&feature=youtube_gdata_player/','',$watch); // get video id
$vid = substr($vid, -11);

- 使用新API格式的一段代码 -

<?php
error_reporting(0);
if ($_COOKIE['autoexp'] == 'on'){$autoexp = "showresult";}else{$autoexp = "hideresult";}
$query = $_POST['terms'];
$searchnum = $_COOKIE['snum'];
$searchurl = "https://www.googleapis.com/youtube/v3/search?q=".$query."&part=snippet&key=MYAPIKEYISHERE&max-results=50&format=5";

$feedURL = $searchurl;                             // set feed URL
$sxml = simplexml_load_file($feedURL);                     // read feed into SimpleXML object

$i = 0;
$i++;
$sn = 1;

if (empty($query)){
    print '<tr><td><div style="margin:auto;padding-top:155px;width:300px;height:120px;text-align:center;"><img src="images/logo.png" height="100"></div></td></tr>';
}

foreach ($sxml->entry as $entry) {                     // iterate over entries in feed
if ($sn <= $searchnum){

    $media = $entry->children('http://search.yahoo.com/mrss/');    // get nodes in media: namespace for media information

    $attrs = $media->group->player->attributes();              // get video player URL
    $watch = $attrs['url']; 

    $attrs = $media->group->thumbnail[0]->attributes();        // get video thumbnail
    $thumbnail = $attrs['url'];

    $yt = $media->children('http://gdata.youtube.com/schemas/2007');   // get <yt:duration> node for video length
    $attrs = $yt->duration->attributes();
    $length = $attrs['seconds']; 

    $yt = $entry->children('http://gdata.youtube.com/schemas/2007');   // get <yt:stats> node for viewer statistics
    $attrs = $yt->statistics->attributes();
    $viewCount = $attrs['viewCount']; 

    $gd = $entry->children('http://schemas.google.com/g/2005');        // get <gd:rating> node for video ratings
    if ($gd->rating) {
        $attrs = $gd->rating->attributes();
        $rating = $attrs['average'];
    }else{$rating = 0;}

    $vid = preg_replace ('/&feature=youtube_gdata_player/','',$watch); // get video id
    $vid = substr($vid, -11);

    if ($length <= 600){
        $i++;
        print " <tr class=\"d".($i & 1)."\"><td>";
        ?>

    <table class="searchresults">
    <tr><td>
        <b>- <a href="#!" onclick="showinfo('<?=$vid?>')"><?php echo $media->group->title; ?></a></b>
    </td><td WIDTH="110">
        <div style="padding-left:7px;"><B>Length : &nbsp;</B> <?=substr(gmdate("i:s", '00'.$length),1)?></div>
    </td><td width="130" align="center">
        <a href="#!" onclick="getit('<?=$vid?>');"><IMG SRC="images/mp3.png" onmouseover="this.src='images/mp32.png';" onmouseout="this.src='images/mp3.png';" title="Get MP3"></a>
    </td></tr>
    </table>
    <div id="infodiv-<?=$vid?>" class="<?=$autoexp?>">
        <table class="searchresults" style="height:25px;" >
        <tr><td>
            <div class="menubar2" style="position:relative;top:-5px;">
                <div style="float:left;padding-left:10px;position:absolute;margin-top:-1px;z-index:5;"><img src="<?=$thumbnail?>" height="42" border="1"></div>
                <div style="float:left;text-align:left;padding-left:85px;color:#333333;"><b>Author:</b> <?php echo $entry->author->name; ?><div style="color:#000000;padding-top:4px;padding-bottom:0px"><small><?=$viewCount?> Views</small></div></div>
                <div style="float:right;margin-right:20px;margin-top:9px;border-style:solid;border-width:1px;"><object width= "217" height="30"><param name="movie" value="http://www.youtube.com/v/<?=$vid?>&rel=0&fs=0&theme=light&showinfo=0&modestbranding=1&autohide=0&color=red"></param><param name="allowFullScreen" value="false"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/<?=$vid?>&rel=0&fs=0&theme=light&showinfo=0&modestbranding=1&autohide=0&color=red" type="application/x-shockwave-flash" allowfullscreen="false" width="217" height="30" allowscriptaccess="always"></embed></object></div>
             </div>
        </td></tr>
        </table>
    </div>
    </td></tr>
    <?php 
    $sn++;
    }else{}

}else{}
}
?>
</table>

我将如何重新开始这项工作?

2 个答案:

答案 0 :(得分:1)

对于视频统计信息,您可以请求

GET https://www.googleapis.com/youtube/v3/videos?part=statistics&id={VIDEO_ID}&key={YOUR_API_KEY}

有关您可以从视频中获得哪种数据的详细信息,请阅读this

您甚至可以使用this尝试此终结点。

答案 1 :(得分:0)

是的,这是因为您需要立即使用他们的官方API并获取您的开发人员密钥(免费)并下载php Google服务库。 https://github.com/google/google-api-php-client

这是一个如何从PHP视频中获取信息的示例

//You need to download these libraries
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';

$client = new Google_Client();

$client->setDeveloperKey("YOURDEVELOPERKEYHERE");
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);

// YouTube object used to make all API requests.
$youtube = new Google_Service_Youtube($client);

$videoIds = "a0nii7mxpFc"; //comma separated if > 1 video

$listResponse = $youtube->videos->listVideos("id,snippet,contentDetails,statistics",array('id' => $videoIds,'maxResults'=>50));

$videoList = $listResponse['items'];                    
foreach ($videoList as $key => $value){
    $video = $videoList[$key];
    //Here are the stats
    echo $video['statistics']['likeCount'];
    echo $video['statistics']['dislikeCount'];
}