ラベル twitterapi の投稿を表示しています。 すべての投稿を表示
ラベル twitterapi の投稿を表示しています。 すべての投稿を表示

2013年9月13日金曜日

REST API v1.1 動作確認

REST API v1.1  Tweets POST statuses/update
一から作成したOauth 認証、API経由で投稿が成功しました。

注意店は status のエンコードです。
    Signature base string 作成時に、rawurlencode します。
    Authorization header 作成時に urlencode します。
ここに区別しないと、日本語の送信にエラーになります。

""{\"errors\":[{\"message\":\"Could not authenticate you\",\"code\":32}]}""






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

<?php
$paras['status']=rawurlencode('テ スト 中 です。');
$url='https://api.twitter.com/1.1/statuses/update.json';
$x=new twoauth('POST',$url,$paras);
$x->oauth_signature();
$x->send_curl();
$x->debug();

?>

<?php

class twoauth {
public $oauth_para=array();
public $oauth_consumer_key= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
public $oauth_consumer_key_secret= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
public $oauth_token= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
public $oauth_token_secret= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
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_url=$url;
$this->request_datas=$datas;
}



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_URL, $this->request_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
if ($this->request_curldata!=null){
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->request_curldata);
}
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>';

}

}




?>