Berikut contoh kode sourde untuk menampilkan video berdasarkan keyword menggunakan php.
<?php
/**
* URL : http://sedotcode.blogspot.com/2017/03/youtube-api-search-by-keyword.html
*/
function ngegrab($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$uaa = $_SERVER['HTTP_USER_AGENT'];
curl_setopt($ch, CURLOPT_USERAGENT, "User-Agent: $uaa");
return curl_exec($ch);
}
function search_video_ytb($kw,$api,$num='5'){
$kw = urlencode($kw);
$grab = ngegrab('https://www.googleapis.com/youtube/v3/search?key=' . $api . '&part=snippet&order=relevance&maxResults='.$num.'&q=' . $kw . '&type=video');
$json = json_decode($grab);
return $json;
}
function detail_video_ytb($api,$idvideo){
return ngegrab('https://www.googleapis.com/youtube/v3/videos?key=' . $api . '&part=contentDetails,statistics&id=' . $idvideo . '');
}
/**
* function search_video_ytb ambil di :https://sedotcode.blogspot.co.id/2017/03/grab-youtube-php-api-v3.html
*/
$api = "YTB_API_KEY"; //https://console.developers.google.com
$kw = "funny cat";
$json = search_video_ytb($kw,$api);
if ($json) {
foreach ($json->items as $value) {
$id_video = $value->id->videoId;
$id_channel = $value->snippet->channelId;
$tgl_publish = $value->snippet->publishedAt;
$name = $value->snippet->title;
$deskripsi = $value->snippet->description;
$nm_channel = $value->snippet->channelTitle;
$thumb_default = "https://i.ytimg.com/vi/$id_video/default.jpg";//120x90
$thumb_meidum = "https://i.ytimg.com/vi/$id_video/mqdefault.jpg";//320x180
$thumb_hight = "https://i.ytimg.com/vi/$id_video/hqdefault.jpg";//480x360
//echo "id video = ".$id_video."<br>";
}
}
Masih ada yang belum paham? tunggu kelanjutannya hanya di blog sedot code php.
0 Response to "Youtube API V3 Search by Keyword PHP "
Posting Komentar