2013年9月14日土曜日

REST API v1.1 動作確認 の続き

修正しました。
GET が動くようになった。

<?php
//date_default_timezone_set('Asia/Tokyo');
//echo '<br>--------timezone------<br>';
//echo date_default_timezone_get();
//echo '<br>--------timezone------<br>';
?>

<?php
//$paras['status']=rawurlencode('GET  動くようになった');
//$paras['since_id']=rawurlencode('14927799');
//$paras['count']=rawurlencode('50');
//$paras['since_id']=rawurlencode('259320959964680190');
//$paras['max_id']=rawurlencode('259320959964680500');
//$paras['q']=rawurlencode("アイドル");
//$paras['q']=rawurlencode('in China');


//$url='https://api.twitter.com/1.1/friends/ids.json';
$url='https://api.twitter.com/1.1/friendships/lookup.json';

$x=new twoauth('GET',$url,$paras);
$x->oauth_signature();
$x->send_curl();
$x->debug();

?>

<?php

class twoauth {
public $oauth_para=array();
public $oauth_consumer_key= '****************************';
public $oauth_consumer_key_secret= '*****************************';
public $oauth_token= '**************************************';
public $oauth_token_secret= '***********************************';
public $oauth_callback= 'http://www.xxx.com';
public $oauth_signature_method= 'HMAC-SHA1';
public $oauth_version= '1.0';
public $useragent = 'TwitterOAuth v0.2.0-beta2';

public $request_datas=array();
public $request_curldata;
public $request_type;
public $request_url;
public $signature_base_string;
public $signature_key;
public $oauth_signature;
public $Authorization_header;
public $curlgetinfo;
public $response_josn;


public function twoauth ($retype='POST',$url,$datas=null){

$this->oauth_para['oauth_consumer_key']=$this->oauth_consumer_key;
$this->oauth_para['oauth_signature_method']=$this->oauth_signature_method;
$this->oauth_para['oauth_token']=$this->oauth_token;
$this->oauth_para['oauth_version']=$this->oauth_version;
$this->oauth_para['oauth_nonce']=md5(microtime().mt_rand());
$this->oauth_para['oauth_timestamp']=time();
$this->oauth_para['oauth_signature'];
$this->signature_key=rawurlencode($this->oauth_consumer_key_secret).'&'.rawurlencode($this->oauth_token_secret);
$this->request_type=$retype;
$this->request_datas=$datas;
$this->request_url=$url;

}



public function oauth_signature(){

$bas_para=array();
if(!empty($this->request_datas) and $this->request_datas!=null){
$bas_para=$this->oauth_para + $this->request_datas;
}else{
$bas_para=$this->oauth_para;
}

ksort($bas_para);
$temp=array();
foreach ($bas_para as $key=>$val){
$temp[count($temp)]=$key.'='.$val;
}
$bas_sting=rawurlencode(implode('&',$temp));

$this->signature_base_string=$this->request_type.'&'.rawurlencode($this->request_url).'&'.$bas_sting;
$this->oauth_signature=rawurlencode(base64_encode(hash_hmac('sha1',$this->signature_base_string,sprintf($this->signature_key),true)));
$this->oauth_para['oauth_signature']=$this->oauth_signature;

ksort($this->oauth_para);
$temp=array();
foreach ($this->oauth_para as $key=>$val){
$temp[count($temp)]=$key.'="'.$val.'"';
}
$this->Authorization_header='Authorization: OAuth '.(implode(', ',$temp));

if (count($this->request_datas)!=0){
ksort($this->request_datas);
$temp=array();
if ($this->request_datas!=''){
foreach ($this->request_datas as $key=>$val){
$temp[count($temp)]=$key.'='.urlencode(rawurldecode($val));
}
}
$this->request_curldata=implode('&',$temp);
}else{
$this->request_curldata=abc;
}
}


public function send_curl(){

$ch=curl_init();
//curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
if ($this->request_curldata!=null){
if ($this->request_type=='POST'){
//POST
curl_setopt($ch, CURLOPT_URL, $this->request_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->request_curldata);
}else if($this->request_type=='GET'){
//GET
if(!empty($this->request_datas)){
curl_setopt($ch, CURLOPT_URL, $this->request_url.'?'.rawurldecode(http_build_query($this->request_datas)));
}else{
curl_setopt($ch, CURLOPT_URL, $this->request_url);
}
}else{
//
exit("not support ($this->request_type) HTTP Methods");
}
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->Authorization_header));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,true);
$this->response_josn = json_decode(curl_exec($ch));
$this->curlgetinfo=curl_getinfo($ch,CURLINFO_HEADER_OUT);
curl_close($ch);
}





public function debug(){

echo '<br><pre>';
echo 'request_datas<br>';
echo var_dump($this->request_datas);
echo '<br><br>';
echo 'Signature base string<br>';
echo $this->signature_base_string;
echo '<br><br>';
echo 'oauth_signature<br>';
echo $this->oauth_signature;
echo '<br><br>';
echo 'Authorization header<br>';
echo $this->Authorization_header;
echo '<br><br>';
echo 'Curl data<br>';
echo $this->request_curldata;
echo '<br><br>';
echo 'Curl_getinfo<br>';
echo var_dump($this->curlgetinfo);
echo '<br><br>';
echo 'JOSN data<br>';
echo var_dump($this->response_josn);
echo '<br><br>';

}

}




?>

0 件のコメント: