2013年9月21日土曜日

POST oauth/access_token

POST oauth/access_token 成功

<?php
//error_reporting(E_ALL);
date_default_timezone_set('Asia/Tokyo');
?>
<?php
//new newlogin(oauth_consumer_key, oauth_consumer_key_secret, Callback_URL);
$x=new newlogin('**********', '*************', 'http://www.dodofei.com/oauthget.php');
if(!array_key_exists('oauth_token',$_REQUEST) and !array_key_exists('oauth_verifier',$_REQUEST)){
$x->request_token();
}else{
$x->access_token("http://www.dodofei.com");
}

?>
<?php
class newlogin{

public $debug='off';
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 $oauth_para=array();
  public $oauth_consumer_key='';
public $oauth_consumer_key_secret= '';
public $oauth_token_secret= '';

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_consumer_key_secret=$oauth_consumer_key_secret;
}

public function request_token(){
//request_token
$signature_key=rawurlencode($this->oauth_consumer_key_secret).'&';
$respons=$this->fun_curl($this->oauth_para, $signature_key, $this->Request_token_URL,'POST');
$requestToken = array();
foreach(explode('&',$respons) as $val){
$param = explode('=',$val);
$requestToken[$param[0]] = $param[1];
}
$this->oauth_token_secret=$requestToken['oauth_token_secret'];
header(sprintf('Location: %s?%s',$this->Authorize_URL,'oauth_token='.$requestToken['oauth_token']));
}

public function authorize(){
//authorize
header(sprintf('Location: %s?%s',$this->Authorize_URL,$respons));
}

public function access_token($hpurl){
//access_token
$signature_key=rawurlencode($this->oauth_consumer_key_secret).'&'.rawurlencode($this->oauth_token_secret);
$add_para=array();
$add_para['oauth_token']=$_GET['oauth_token'];
$add_para['oauth_verifier']=$_GET['oauth_verifier'];
$para=$this->oauth_para+$add_para;
$respons=$this->fun_curl($para, $signature_key, $this->Access_token_URL,'POST');

foreach (explode('&',$respons) as $val){
$param=explode('=',$val);
setcookie($param[0],$param[1],time()+60*60*24*30*30);
}

if($respons!='error'){
//header('Location: http://www.dodofei.com/index.php?'.$respons);
header('Location: '.$hpurl);
}
}



public function fun_curl($para, $signature_key, $url, $httptype){
//oauth_signature
ksort($para);
$temp=array();
foreach ($para as $key=>$val){
if(!empty($val)){
$temp[count($temp)]=$key.'='.rawurlencode($val);
}
}

$bas_sting=implode('&',$temp);
$signature_base_string=rawurlencode($httptype).'&'.rawurlencode($url).'&'.rawurlencode($bas_sting);

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

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

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

//Authorization: OAuth
ksort($para);
$temp1=array();
$temp2=array();

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

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

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

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


//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($Authorization_header,'Content-Length:','Expect:','Content-Tpye:'));
//curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->Authorization_header));

if($httptype=='GET'){
//GET動かない
$geturl=$url.'?'.$request_curldata;
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_curldata);
curl_setopt($ch, CURLOPT_URL, $geturl);
curl_setopt($ch, CURLOPT_HTTPGET, true);
}

if($httptype=='POST'){
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, $url);
}

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


if($curlgetinfo['http_code']==200){
return $response_josn;
}else{
return 'error';
}
}

}


?>

0 件のコメント: