其實早期有用過plurk API 1.0 寫過透可機器人 ,那是一個噗浪很盛行的年代(2010年) 好友數量暴增到了近兩千人,還因為此修改過幾次程式讓程式的執行速度快一點。而隨著噗浪開始落寞( 創辦人ALVIN表示:幹! ) 而我後來又跑去當兵沒時間維護的情況之下,這機器人也隨著噗浪不再支援API 1.0 而暫停了一陣子。
噗浪現在已經採用PLURK API 2.0了(其實採用很久了)而2.0主要是改用OAuth認證機制(wiki) 包括facebook 跟google、dropbox等大站都是用這個認證功能的
我相信這種圖各位應該已經熟悉到不行了(應該也沒到多熟悉拉) 這就是oauth認證的方式
而在你開始寫plurk機器人之前你必須先獲得以下四個參數
這幾個參數在plurk api那邊可以獲得(可參閱這篇別站文章 我就不再多廢話了http://zh.blog.plurk.com/archives/1121)
當你得到這四個參數後,就可以直接執行下列範例程式就可以跑了
plurk.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <!--?php set_time_limit(0); function do_action( $url , $new_parms = array ()) { $oauth_consumer_key = "" ; //你的consumer_key talk $oauth_consumer_secret = ""; //你的consumer_secret $oauth_token = ""; //你的tokeny_key $oauth_token_secret = ""; //你的token_secret // global $oauth_consumer_key,$oauth_token,$oauth_consumer_secret,$oauth_token_secret; $oauth_nonce = rand(10000000,99999999); $oauth_timestamp = time(); $parm_array = array("oauth_consumer_key"=-->$oauth_consumer_key,"oauth_nonce"=>$oauth_nonce,"oauth_consumer_key"=>$oauth_consumer_key,"oauth_signature_method"=>"HMAC-SHA1","oauth_timestamp"=>$oauth_timestamp,"oauth_token"=>$oauth_token,"oauth_version"=>"1.0"); $parm_array = array_merge ( $parm_array , $new_parms ); $base_string = sort_data( $parm_array ); $base_string = "POST&" .rawurlencode( $url ). "&" .rawurlencode( $base_string ); $key = rawurlencode( $oauth_consumer_secret ). "&" .rawurlencode( $oauth_token_secret ); $oauth_signature = rawurlencode( base64_encode (hash_hmac( "sha1" , $base_string , $key ,true))); $parm_array = array_merge ( $parm_array , array ( "oauth_signature" => $oauth_signature )); $ch = curl_init(); curl_setopt( $ch , CURLOPT_URL, $url ); curl_setopt( $ch , CURLOPT_POST, true); curl_setopt( $ch ,CURLOPT_RETURNTRANSFER,TRUE); curl_setopt( $ch , CURLOPT_POSTFIELDS,sort_data( $parm_array )); $data = curl_exec( $ch ); curl_close( $ch ); return json_decode( $data ,TRUE); } function sort_data( $data ) { ksort( $data ); $string = "" ; foreach ( $data as $key => $val ) { if ( $string == "" ) { $string = $key . "=" . $val ; } else { $string .= "&" . $key . "=" . $val ; } } return $string ; } // ?> |
記得把你的那四個參數填進去
index.php
1 2 3 4 5 6 | <!--?php include ( "plurk.php" ); $qulifier = 'says' ; $message = "今日噗浪科技日報內容!" ; //發噗 $post_info = do_action("http://www.plurk.com/APP/Timeline/plurkAdd",array("content"=---->rawurlencode($message),"qualifier"=>$qulifier)); //回噗 $reply = "回噗內容" ; do_action( "http://www.plurk.com/APP/Responses/responseAdd" , array ( "content" =>rawurlencode( $reply ), "qualifier" => $qulifier , "plurk_id" => $post_info [plurk_id])); ?> |
執行index.php就可以了
上面就有看到有demo發噗跟回噗的範例了
如果你還要其他功能的話http://www.plurk.com/API#/ 這篇會有寫各功能怎用
你只要把
"http://www.plurk.com/APP/Timeline/plurkAdd"
改成你想要的功能的網址,然後根據噗浪api說要回傳那些參數
例如
content: The Plurk’s text.
qualifier: The Plurk’s qualifier, must be in English.
你就把他寫成下面這樣
array("content"=>rawurlencode($message),"qualifier"=>$qulifier)
這邊的參數,看你要的那項功能需要回傳那些參數值回去,就把它改成你想要的參數即可
噗浪有提供test console 可以給你參考需要那些變數,大致上這樣就可以run一個簡單的噗浪2.0 機器人了
範例程式碼我放在github上 ->範例
延伸閱讀: 淺談 oAuth 2.0
您好,想請問放置 php 檔案的空間有什麼限制嗎?
因為執行上沒有錯誤但是並沒有成功發噗…
應該是沒有耶 你可以先在你自己的電腦跑看看阿 他主要是用curl 這個php的模組 你先去看看你有沒有載入這個模組 不然通常有錯你畫面會顯示
你好,我想請問一下,若將這個範例改寫成上傳檔案 (http://www.plurk.com/APP/Timeline/uploadPicture)
要怎麼改寫呢?我是很久都沒有成功過….附上 plurk.php 與 index.php
plurk.php
https://gist.github.com/peter279k/f433dc6cc052e37199d50930dd66afa6
index.php
https://gist.github.com/peter279k/dd8802ed4226375e9424d417743bb04a