<?php
class YoutubeV3{	
	function __construct(){
		//$this->authsubkey = 'AIzaSyDYnR4VAaf-041F5RKt9xIFEX9SEImrCjQ';
		//$this->authkey = 'AIzaSyA1Yr4Zogk_BWSNTKbzTHRfqA4XVTJn6QU';		
		$this->authkey = 'AIzaSyDbrnd06mw7m3TfhGAPRLGLsT7xgxuB1HM';
		$this->apiroot = 'https://www.googleapis.com/youtube/v3/';
	}	
	function getVideo($videoid){
		$key = $this->authkey;
		$part = $this->part;
		$url = $this->apiroot."videos?key=".$key."&part=snippet,contentDetails,statistics&id=".$videoid;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$content = curl_exec($ch);
		curl_close($ch);
		$result = json_decode($content, true);
		if($result[pageInfo][totalResults]>0 && isset($result[error])!=1){
			unset($result[items][0][contentDetails][regionRestriction]);
			$youtubeinfo[result] = 'success';
			$youtubeinfo[title] =  $result[items][0][snippet][title];
			$youtubeinfo[snippet] = $result[items][0][snippet];
			$youtubeinfo[contentDetails] = $result[items][0][contentDetails];
			$youtubeinfo[statistic] = $result[items][0][statistics];
		}else{
			$youtubeinfo[result] = $result;	
		}
		return $youtubeinfo;			
	}
	function getChannel($channel_id){
		$key = $this->authkey;
		$url = $this->apiroot."channels?key=".$key."&part=snippet&id=".$channel_id;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$content = curl_exec($ch);
		curl_close($ch);
		$result = json_decode($content, true);
		if($result[pageInfo][totalResults]>0 && isset($result[error])!=1){
			$channelinfo[result] = 'success';	
			$channelinfo[title] = $result[items][0][snippet][title];
			$channelinfo[thumbnails] = $result[items][0][snippet][thumbnails];
		}else{
			$channelinfo[result] =$result;	
		}
		return $channelinfo;		
	}
	function getPlaylistItem($playlist_id, $maxResults = 10){
		$key = $this->authkey;
		$url = $this->apiroot."playlistItems?key=".$key."&part=snippet&maxResults=".$maxResults."&playlistId=".$playlist_id;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$content = curl_exec($ch);
		curl_close($ch);
		$searchResult = json_decode($content, true);
		if($searchResult[pageInfo][totalResults]>0 && isset($searchResult[error])!=1){
			$playlistItem[result] = 'success';	
			foreach($searchResult[items] as $i){
				$video = $this->getVideo($i[snippet][resourceId][videoId]);
				$video[videoid] = $i[snippet][resourceId][videoId];
				if($video[result] =='success')
					$tracks[] = $video;
				else
					reply_json($video);			
			}
			$result[tracks] = $tracks; 
		}else{
			$result[result] =$searchResult;	
		}
		return $result;		
	}	
	/*
		order 종류
		- relevance : 검색 query와 관련성 높은 리소스로 정렬
		- viewcount : 조회수가 높은 리소스로 정렬 
		- title : 제목에 따라 문자순으로 정렬
		그외 종류는 youtube api 공홈가서 확인!
		
		type 종류
		- video
		- channel
		- playlist 
	*/
	function search($q , $order = "relevance", $type ="video", $maxResults = 10){
		$key = $this->authkey;
		$url = $this->apiroot."search?key=".$key."&type=".$type."&order=".$order."&part=snippet&maxResults=".$maxResults."&q=".$q;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$content = curl_exec($ch);
		curl_close($ch);
		$searchResult = json_decode($content, true);
		
		if($searchResult[pageInfo][totalResults]>0 && isset($searchResult[error])!=1){
			$result[result] = 'success';	
			foreach($searchResult[items] as $i){
				$video = $this->getVideo($i[id][videoId]);
				$video[videoid] = $i[id][videoId];
				if($video[result] =='success'){
					$tracks[] = $video;
				}else{
					reply_json($video);
				}	
			}
			$result[tracks] = $tracks;
		}else{
			$result[result] = 'fail';
			$result[msg] = $searchResult;	
		}
		return $result;		
	}	
}
?>