2014年4月29日火曜日

YouTube Data API 3.0 その1

Function Youtube API3.0



<?php
error_reporting(0);
date_default_timezone_set('Asia/Tokyo');
//session_start();
$_SESSION['req_url'] = 'http://'.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];

if(array_key_exists('you_access_token',$_COOKIE)){
$token = $_COOKIE['you_access_token'];
}else{
$_SESSION['req_url'] = 'http://'.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
header('location: http://www.xxxxxxx.com/google/oauth.php');
}

?>
<?php
//YouTube Data API 3.0
//Searchメソッド
set_time_limit(0);
$xx = new dataapi;
$xx->token_check(); //
//$xx->c_play_list('本語 対応しますか'); //再生リストの作成、公開 名前の入力が必須 return list id;
//$xx->c_play_item_insert(Lid,Vid); //
//$xx->search('Part Of Me - Katy Perry'); //サーチ return Video ID array[0[;





class dataapi{

public function dataapi(){



}

public function token_check(){

if(array_key_exists('you_access_token',$_COOKIE)){
$token = $_COOKIE['you_access_token'];
}else{
$_SESSION['req_url'] = 'http://'.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
header('location: http://www.xxxxxxx.com/google/oauth.php');
}
}


public function search($q){
//API リクエストで指定したクエリ パラメータに一致する検索結果のコレクションを返します。
$request_url = 'https://www.googleapis.com/youtube/v3/search';
$mode = 'GET';
$para['part'] = 'id'; //必須パラメータ
$para['q'] = $q;
$para['type'] = 'video';

$curl_url = $request_url.'?'.http_build_query($para);

$temp = $this->curl($mode,$curl_url);

return $temp['items']['0']['id']['videoId'];

//echo '<pre>';
//echo 'search';
//print_r($temp);
}


public function c_play_list($name){
//playlist リソースは YouTube の再生リストを表します。
//再生リストを作成します   Playlists: insert

$request_url ='https://www.googleapis.com/youtube/v3/playlists';
$mode = 'POST';
$para['part'] = 'snippet,status';
$paras['snippet'] = array('title' => $name);
$paras['status'] = array ('privacyStatus' => 'public');

$curl_url = $request_url.'?'.http_build_query($para);

$temp = $this->curl($mode,$curl_url,$paras);

//echo '<br>';
// print_r($temp);


return $temp['id'];
}






public function c_play_item_insert($Lid,$Vid){
//playlist リソースは YouTube の再生リストを表します。
//作成したリストにアイテムを追加

$request_url ='https://www.googleapis.com/youtube/v3/playlistItems';
$mode = 'POST';
$para['part'] = 'snippet';
$paras['snippet'] = array('playlistId' => $Lid,
 'resourceId' => array('videoId' => $Vid,
'kind' => 'youtube#video'));

$curl_url = $request_url.'?'.http_build_query($para);
$temp = $this->curl($mode,$curl_url,$paras);
//echo '<pre>';
//print_r($temp);

}




public function curl($mode,$url,$paras=null){
//cURL 送信
$Authorization_header = 'Authorization: Bearer  '.$_COOKIE['you_access_token'];
if ($mode == 'GET'){
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,true);

curl_setopt($ch, CURLOPT_HEADER, false);
//curl_setopt($ch, CURLINFO_HEADER_OUT, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, array($Authorization_header));
curl_setopt($ch, CURLOPT_URL, $url);
$code = curl_exec($ch);
//$curlgetinfo=curl_getinfo($ch,CURLINFO_HEADER_OUT);
//$curlgetinfo=curl_getinfo($ch);
//echo '<pre>';
//var_dump($curlgetinfo);

curl_close($ch);
}

if ($mode == 'POST'){
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,true);

curl_setopt($ch, CURLOPT_HEADER, false);
//curl_setopt($ch, CURLINFO_HEADER_OUT, true);

//curl_setopt($ch, CURLOPT_HTTPHEADER, array($Authorization_header,'Content-Length:','Expect:','Content-Tpye: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: ','Content-Type:  application/json',$Authorization_header, 'X-JavaScript-User-Agent:  Google APIs Explorer'));
//echo '<pre>';

$data_string = json_encode($paras);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_URL, $url);
$code = curl_exec($ch);
$curlgetinfo=curl_getinfo($ch,CURLINFO_HEADER_OUT);
//$curlgetinfo=curl_getinfo($ch);
//echo '<pre>';
//var_dump($curlgetinfo);
curl_close($ch);
}


return json_decode($code,true);






}


}

0 件のコメント: