php 부분
<?php
function CurlGet( $url ){
$curlsession = curl_init ();
curl_setopt($curlsession, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt ($curlsession, CURLOPT_URL, $url);
curl_setopt ($curlsession, CURLOPT_HTTPGET, TRUE);
curl_setopt ($curlsession, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($curlsession, CURLOPT_HTTPHEADER, $headers);
$res = curl_exec ($curlsession);
return $res;
}
$Chnelurl =
'https://www.googleapis.com/youtube/v3/playlistItems?playlistId=재생목록ID&key=나의 KEY&part=snippet&fields=*&maxResults=50';
$res = CurlGet($Chnelurl);
?>
---------------------------------------------------------------------------------------------
html 부분
<table>
<tr>
<th>img</th>
<th>title</th>
<th>ViewCount</th>
<th>regdate</th>
</tr>
<tbody id="getVideo"></tbody>
</table>
---------------------------------------------------------------------------------------------
script 부분
<script>
var div ;
$(function () {
var res = <?=$res?>;
var count = Object.keys(res.items).length;
var videoId, videoImg, videotitle, Viewurl, qwer;
for(var i = 0; i<count; i++){
videotitle = res.items[i].snippet.title;
videoId = res.items[i].snippet.resourceId.videoId;
videoImg = res.items[i].snippet.thumbnails.default.url;
url = 'https://www.googleapis.com/youtube/v3/videos?id='+videoId+'&key=나의 KEY&part=statistics,snippet&fields=*';
ViewUrl(url,videoId, i);
$("#getVideo").append(
'<tr><td><img src="'+videoImg+'"></td><td><a href="http://www.youtube.com/watch?v='+videoId+'">'+videotitle+'</a></td><td id="countvideo_'+i+'"></td><td id="regdate_'+i+'"></td></tr>'
);
}
});
function ViewUrl(urls,vid,count){
$.ajax({
url: urls,
dataType: "json",
success: function (data) {
console.log(data);
if(vid == data.items["0"].id){
var dataviews = data.items["0"].statistics.viewCount;
var regdate = data.items["0"].snippet.publishedAt;
var regdates = regdate.split('T');
$("#countvideo_"+count).html(dataviews + " view");
$("#regdate_"+count).html(regdates[0]);
}
},
error:function(request,status,error){
alert("code:"+request.status+"\n"+"message:"+request.responseText+"\n"+"error:"+error);
}
});
}
</script>
---------------------------------------------------------------------------------------------
ViewUrl.php
<?php
function CurlGet( $url ){
$curlsession = curl_init ();
curl_setopt($curlsession, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt ($curlsession, CURLOPT_URL, $url);
curl_setopt ($curlsession, CURLOPT_HTTPGET, TRUE);
curl_setopt ($curlsession, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($curlsession, CURLOPT_HTTPHEADER, $headers);
$res1 = curl_exec ($curlsession);
return $res1;
}
$Viewurl = $_POST['url'];
$res1 = CurlGet($Viewurl);
echo '<pre>'.$Viewurl.'</pre>';
?>
결과물
'API' 카테고리의 다른 글
[GoogleCharts] ComboChart 예제 (0) | 2017.07.21 |
---|