2015-09-26 78 views
1

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

因为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/'); //得到媒体的节点:随着新的API格式ONE本部命名为媒体信息

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

-old CODE

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

我怎么会去再次使这项工作?

回答

1

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

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

有关什么样的数据,你可以从视频获得更多的信息,请阅读this

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

+0

获取信息现在,说这是我想要的搜索视频的情况下,说:“马跑”我该怎么做? –

+0

现在我被我的旧代码卡住了,我不知道如何转换它(上面会发布) –

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']; 
} 
+0

@ Berriel的方法更简单快捷,但是如果你只是想要,那么使用这个库会给你其他更多的功能。如果你只是想要,我会强烈推荐Berriel's。 –

+0

修改我的帖子,这虽然有帮助! –