CodeIgniter Curl library 是將php的curl function 寫成一個library方便於在 CodeIgniter 上使用,雖然可以直接在CodeIgniter裡面寫curl function,但是Curl library已經打包好了不需要重覆寫相同的程式,另外後面是以登入plurk為範例,所以必須先取得 plurk api key喔!
準備
1. 下載 curl libraries 並放置 libraries 資料夾裡
2. 設定自動載入 curl libraries
// 修改 config/autoload.php $autoload['libraries'] = array('curl'); |
範例
一 登入 PLURK
1 2 | $responce = $this->curl->simple_post('http://www.plurk.com/API/Users/login', array('api_key'=>'xxxx','username'=>'xxxx','password'=>'xxxx')); print_r(json_decode($responce)); |
二 登入 PLURK save cookie
1 2 3 4 5 6 7 8 9 10 | $_options = array( CURLOPT_COOKIESESSION => true, CURLOPT_COOKIEFILE => './cache/cookie', CURLOPT_COOKIEJAR => './cache/cookie', CURLOPT_USERAGENT => "php-plurk-api agent" ); $this->curl->create('http://www.plurk.com/API/Users/login'); $this->curl->options($_options); $this->curl->post(array('api_key'=>'xxxx','username'=>'xxxx','password'=>'xxxx')); print_r(json_decode($this->curl->execute())); |