2016-04-30 149 views
2

有沒有什麼辦法可以檢查YouTube上的某些視頻是否被用戶通過YouTube API觀看?我不只是想知道用戶是否點擊了視頻,但他是否真的看過它。我基本上希望在瀏覽看到YouTube的YouTube時,可以在視頻縮略圖中看到這些信息。如何檢查用戶是否通過YouTube API觀看視頻

+0

安置自己的attemept –

+1

我沒有找到的YouTube API文檔中的任何API端點,將返回這樣的信息 – Dejvid

+0

用戶需要使用OAuth進行身份驗證或沒有?這很重要 – mpgn

回答

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; 
}` 
+1

您能解釋這些代碼嗎?我可以看到您正在處理/ youtue/v3/videos的請求,但並未告知您用戶是否觀看過該視頻。 – Dejvid

+0

同意@Dejvid(和downvoted):這列出了通過'$ vid'傳遞的視頻的詳細信息,但沒有提及用戶最近觀看的內容。 「最近觀看」似乎無法通過當前的API。 – kyle

相關問題