首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >gdata的替换

gdata的替换
EN

Stack Overflow用户
提问于 2015-09-26 08:10:51
回答 2查看 110关注 0票数 1

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

既然gdata不再起作用了,我现在该怎么做呢?我在接口里找不到!('http://gdata.youtube.com/schemas/2007');//获取观众统计信息的节点http://gdata.youtube.com/feeds/api/videos/?q=我不太确定该怎么办

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

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

代码语言:javascript
复制
$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格式的-OLD代码-

代码语言:javascript
复制
<?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>

我该如何让它再次工作呢?

EN

回答 2

Stack Overflow用户

发布于 2015-09-26 08:38:45

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

代码语言:javascript
复制
GET https://www.googleapis.com/youtube/v3/videos?part=statistics&id={VIDEO_ID}&key={YOUR_API_KEY}

有关你可以从视频中获得的数据类型的更多信息,请阅读this

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

票数 1
EN

Stack Overflow用户

发布于 2015-09-26 08:40:06

是的,那是因为你现在需要使用他们的官方API,获取你的开发者密钥(免费)并下载php Google服务库。https://github.com/google/google-api-php-client

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

代码语言:javascript
复制
//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'];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32792297

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档