如何检查用户是否通过YouTube API观看了视频

时间:2016-04-30 15:50:50

标签: youtube-api youtube-data-api youtube-javascript-api

有没有办法检查用户是否通过youtube API观看了youtube上的某些视频?我不仅想知道用户是否点击了视频,而且他是否真的看过它。我基本上想要你在浏览youtube时可以在视频缩略图上看到的信息。

1 个答案:

答案 0 :(得分:1)

这里是我的代码,我使用的是javascript和youtube api v3和php。希望它能帮到你

`    protected function getHistory($vid){
    $return = new stdClass();
        $videos = $vid;
         $vids[] =$vid;

        $videos = explode("\n", $videos);       
        $videos = implode(",", array_map("rtrim", $videos));
                $part = "snippet,contentDetails,statistics";
        $this->url = "https://www.googleapis.com/youtube/v3/videos?key=    {$this->key}&part={$part}&id={$videos}";
                $data = $this->http(TRUE);
        $html ="<h2 class='header'>Your Recently Viewed Videos</h2> <a href='#' class='header' onclick='clearhistory();'>Clear History</a>";
        $html .= "<ul class='media' id='mediacontent'>";
        $vids = array();


        foreach ($data->items as $yt) {
        $html .= $this->listhisVideo($yt);
        $vids[] = $yt->id;
    }
    $html .= "</ul>";       
    $html .=$this->ads(728);



    $html .= '<script type="text/javascript">
        var videos = ["'.implode('","',$vids).'"];
    </script>';

    if(!isset($_POST['his'])) {

    $html ="<h2 class='header'>Your Recently Viewed Videos</h2>";
    $html .='<ul class="media"><li class="video" id="video-EoCz3Vx1pXg">

<center><img src="/img/load.gif"></center>

</li></ul>';
    $html .= '<form id="history" action="/history" method="POST">
<input type="hidden" id= "his" name="his"       value="NqucyQdiFRM,EoCz3Vx1pXg,FJ55SHCzt88" />

</form><script type="text/javascript">
if (localStorage.getItem("videos") === null) {
document.getElementById("his").value = "empty";  
} else {
document.getElementById("his").value = localStorage.getItem("videos");


}

document.getElementById("history").submit();
</script>';





    } 

    if ($_POST['his']=='empty'){
    $html ="<h2 class='header'>Your Recently Viewed Videos</h2>";
    $html .="NO Video Exist";

    }


    $return->html = $html;
    return $return;
}`