Replacement for gdata



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

我应该怎么做这些现在,因为gdata不再工作?我在API中找不到它!("http://gdata.youtube.com/schemas/2007");//获取查看器统计信息的节点http://gdata.youtube.com/feeds/api/videos/?q=我不太确定该怎么办

代码——孩子这就是我的问题所在。我需要弄清楚这是如何编写新的API,

$media = $entry->children('http://search.yahoo.com/mrss/');//在media: namespace中获取媒体信息

$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>

我该如何重新开始工作呢?

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

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

关于你可以从视频中获得什么样的数据的更多信息,阅读这个。

是的,那是因为你现在需要使用他们的官方API并获得你的开发者密钥(它是免费的)并下载php谷歌服务库。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'];
}

相关内容

  • 没有找到相关文章

最新更新