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>';

}

}




?>

2013年9月8日日曜日

Example request (Authorization header has been wrapped):

POST /oauth2/token HTTP/1.1
Host: api.twitter.com
User-Agent: My Twitter App v1.0.23
Authorization: Basic eHZ6MWV2RlM0d0VFUFRHRUZQSEJvZzpMOHFxOVBaeVJn
                     NmllS0dFS2hab2xHQzB2SldMdzhpRUo4OERSZHlPZw==
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
Content-Length: 29
Accept-Encoding: gzip

grant_type=client_credentials

oauth_signature

oauth_signature

$signature_key=$oauth_consumer_key_secret.'&'.$oauth_token_secret;

echo urlencode(base64_encode(hash_hmac('sha1', $sin, $signature_key,true)));

2013年9月2日月曜日

Apache,PHP,MySQL のダウンロード

Apache,PHP,MySQL のダウンロード

Apache ダウンロード

http://httpd.apache.org/


http://www.apachelounge.com/






PHP ダウンロード

http://www.php.net/
http://windows.php.net/download/













PHP をダウンロードするときの「スレッドセーフ」の意味は?
スレッドセーフである、というのは、Windows 上の Apache 2 のような マルチスレッドのウェブサーバーでも動作するバイナリだという意味です。 スレッドごとにローカルストレージのコピーを作成し、 別のスレッドとデータの衝突を起こさないようにしています。

それで、結局どっちを選べばいいのかですって? もし PHP を CGI として動かすつもりなら、スレッドセーフでなくてもかまいません。 リクエストのたびにバイナリが起動するからです。IIS5 や IIS6 といったマルチスレッドのウェブサーバーで動かす場合は、 スレッドセーフ版の PHP を選ばなければなりません。


注意:

VC9 版は Visual Studio 2008 でコンパイルしており、パフォーマンスや安定性が優れています。 VC9 版を使うには、 » Microsoft 2008 C++ Runtime (x86) あるいは » Microsoft 2008 C++ Runtime (x64) をインストールしなければなりません。


http://jp1.php.net/manual/ja/install.windows.manual.php

http://download.microsoft.com/download/1/2/2/1220f3be-11af-4695-990e-5404763d9e9d/vcredist_x86.exe
Microsoft Visual C++ 2008 再頒布可能パッケージ (x86)



MySQL Community Server (GPL)

http://dev.mysql.com/downloads/












2013年9月1日日曜日

Apacheでファイル一覧を表示させない

Apacheでファイル一覧を表示させない

httpd.confを編集,下記の項目

Options Indexes FollowSymLinks          変更前

Options -Indexes FollowSymLinks        変更後



<Directory "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.2/mod/core.html#options
    # for more information.
    #
    Options -Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride None

    #
    # Controls who can get stuff from this server.
    #
    Order allow,deny
    Allow from all

</Directory>

2013年8月28日水曜日

Flickr ファイル検索、Javascript, 前回の続き、検索した結果の写真を表示するようにします

Flickr ファイル検索、Javascript, 前回の続き、検索した結果の写真を表示するようにします。

Google Chromeご利用ください。

2013年8月26日月曜日

Flickr ファイル検索、Javascript,


1.
     key=e2d7f5575339924addac8fd8a3584035
     を入れ替えが必要です。下記のリンクをご参考ください。
     http://www.flickr.com/services/api/explore/flickr.photos.search

2.
     WindowsXDomainRequest() responseText取得したデータの取り扱い
     は不明、要確認。



<html>
<meta>
</meta>
<body>

<form id="flickr">
<input type="text" value="写真" onblur="ffun(this)"/><p>キーワード入力</p><br>
</form>

<script type="text/javascript">
function ffun(val){
var key=val.value;
link(key);
}

function link(key){
key=encodeURIComponent(key);
var url1="http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=e2d7f5575339924addac8fd8a3584035&text="+key+"&format=rest";

if(navigator.userAgent.indexOf('MSIE')>-1){
winxxml(url1);
}else{
xxxml(url1);
}
}

function winxxml(ulink){
var date;
alert(ulink);
date=new XDomainRequest();
date.open('GET',ulink);
date.send();
alert(date.responseText);
}

function xxxml(ulink){
var date;
date=new XMLHttpRequest();
date.open('get',ulink,true);
date.send(null);
date.onreadystatechange=function(){
var ll=date.responseXML.getElementsByTagName('photo');
for(var i=0;i<ll.length;i++){
var aa=ll[i].getAttribute('farm');
var bb=ll[i].getAttribute('server');
var cc=ll[i].getAttribute('id');
var dd=ll[i].getAttribute('secret');
imglink(aa,bb,cc,dd);
}
}
}

function imglink(a,b,c,d){
 document.write('<br>');
 document.write('http://farm'+a+'.staticflickr.com/'+b+'/'+c+'_'+d+'.jpg');
}
</script>
</body>
</html>