2013年9月20日金曜日

GET oauth/authorize 成功

GET oauth/authorize 成功

<?php
error_reporting(E_ALL);
date_default_timezone_set('Asia/Tokyo');
?>
<?php

$x=new newlogin('***************','********************','http://www.********.com');
//http://www.********.com/?oauth_token=z4MV1x5JnEyaRaq5VbpylGoZETAX6XVv5USpZJ247N8&oauth_verifier=5eFa14MMF5DWi8LK3ZsOsyMkg7WinBGwxXNvrNUqKhE
//http://www.********.com/?denied=7ejbFPRb1oZ1Z08r8XQOauRdIeh4EkLWKH0xNFI

?>
<?php


class newlogin{

public $debug='off';
public $request_type='POST';
public $oauth_para=array();
public $Request_token_URL='https://api.twitter.com/oauth/request_token';
public $Authorize_URL='https://api.twitter.com/oauth/authorize';
public $Access_token_URL='https://api.twitter.com/oauth/access_token';
public $oauth_signature_method= 'HMAC-SHA1';
public $oauth_version= '1.0';

public $Authorization_header;
public $signature_base_string;
public $oauth_signature;
public $signature_key;
public $curlgetinfo;
public $response_josn;
public $request_curldata;


public function newlogin($oauth_consumer_key,$oauth_consumer_key_secret,$Callback_URL){

$this->oauth_para['oauth_callback']=$Callback_URL;
$this->oauth_para['oauth_consumer_key']=$oauth_consumer_key;
$this->oauth_para['oauth_nonce']=md5(microtime().mt_rand());
$this->oauth_para['oauth_signature_method']=$this->oauth_signature_method;
$this->oauth_para['oauth_timestamp']=time();
$this->oauth_para['oauth_version']=$this->oauth_version;
$this->oauth_para['oauth_signature']='';
$this->signature_key=rawurlencode($oauth_consumer_key_secret).'&';

$this->oauth_set();
$this->send_curl();
}

public function oauth_set(){
//oauth_signature
ksort($this->oauth_para);
$temp=array();
foreach ($this->oauth_para as $key=>$val){
if(!empty($val)){
$temp[count($temp)]=$key.'='.rawurlencode($val);
}
}

$bas_sting=implode('&',$temp);
$this->signature_base_string=rawurlencode($this->request_type).'&'.rawurlencode($this->Request_token_URL).'&'.rawurlencode($bas_sting);

if($this->debug=='on'){
echo '<pre>';
echo __LINE__;
print_r($this->signature_base_string);
}

// NG $this->oauth_para['oauth_signature']=rawurlencode(base64_encode(hash_hmac('sha1',$this->signature_base_string,$this->signature_key,true)));
$this->oauth_para['oauth_signature']=base64_encode(hash_hmac('sha1',$this->signature_base_string,$this->signature_key,true));
$this->oauth_signature=$this->oauth_para['oauth_signature'];

if($this->debug=='on'){
echo '<pre>';
echo __LINE__;
print_r($temp);
}

//Authorization: OAuth
ksort($this->oauth_para);
$temp1=array();
$temp2=array();

foreach ($this->oauth_para as $key=>$val){
if(!empty($val)){
$temp1[count($temp1)]=$key.'="'.urlencode($val).'"';
if ($key!='oauth_callback'){
$temp2[count($temp2)]=$key.'='.urlencode(rawurldecode($val));
}
}
}

$this->Authorization_header='Authorization: OAuth '.implode(', ',$temp1);

if($this->debug=='on'){
echo '<pre><br>';
echo __LINE__;
print_r($temp1);
}

//GET利用しない
$this->request_curldata=implode('&',$temp2);
if($this->debug=='on'){
echo '<pre><br>';
echo __LINE__;
print_r($temp2);
}

}

public function send_curl(){
//CURL 送信
$ch=curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,true);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->Authorization_header,'Content-Length:','Expect:','Content-Tpye:'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->Authorization_header));

if($this->request_type=='GET'){
//GET動かない
$geturl=$this->Request_token_URL.'?'.$this->request_curldata;
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->request_curldata);
curl_setopt($ch, CURLOPT_URL, $geturl);
curl_setopt($ch, CURLOPT_HTTPGET, true);
}

if($this->request_type=='POST'){
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, $this->Request_token_URL);
}

$this->response_josn = curl_exec($ch);
$this->curlgetinfo=curl_getinfo($ch,CURLINFO_HEADER_OUT);
$this->curlgetinfo=curl_getinfo($ch);
curl_close($ch);

if($this->curlgetinfo['http_code']==200){
header(sprintf('Location: %s?%s',$this->Authorize_URL,$this->response_josn));
echo 'url Send';
}else{
sleep(3);
$this->oauth_set();
$this->send_curl();
echo 'url Error';
}
}



public function debug(){

//Debug
echo '<br>/////////Debut///////////////////';
echo '<br><pre>';
echo 'request_datas<br>';
//echo print_r($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 print_r($this->curlgetinfo);
echo '<br><br>';
echo 'JOSN data<br>';
echo print_r($this->response_josn);
echo '<br><br>';
echo '/////////Debut///////////////////';

}


}





?>

0 件のコメント: