[PHP Framework] How to implement Plurk API in CodeIgniter

ci_logo2 (by appleboy46) 很開心在12月看到 Plurk Release API 出來,而自己也跟網路上一些朋友合作開發 PHP implementation of Plurk API,Plurk (簡稱噗浪)在台灣這一兩年紅了起來,網路高手分別針對噗浪研究產生非官方的 API,現在官網 Release 出來,提供了 JavaPython 的 Example,我想因為 Plurk 是用 Python 寫出來的,所以提供了範例,但是 API 出來沒多久,roga 就集合了網路一些強者,一起開發了 PHP Plurk API,我也拿了此 API 在實作到 CodeIgniter Framework,讓在使用此套 open source 的使用者可以享用 Plurk API。不過從2009.12.29日之後,Plurk 官網有限制每天只能 call 50.000 次,已經蠻多了,不要操掛 Plurk 的機器阿。 1. 首先下載 CodeIgniter 1.7.2 版本:下載 2. 新增檔案 plurk_config.php 和 plurk_constant.php 到 application/config/ 目錄底下 plurk_config.php 檔案內容如下:
 
api_key 請到 Plurk API 網站申請,申請過後會寄信到您的信箱,username 是 Plurk 帳號,password 是 Plurk 密碼。 plurk_constant.php http://github.com/appleboy/CodeIgniter-Plurk-API/blob/master/config/plurk_constant.php 這檔案不需要修改什麼,唯一要注意的是,可以設定 Plurk 的 log 跟 Cookie 檔案位置,目前是存放到 application/logs/ 目錄,如果沒有此目錄,請麻煩建立此資料夾,之後設定 755 讓 Web 可以存取。
define('PLURK_COOKIE_PATH', APPPATH . 'logs/cookie');
define('PLURK_LOG_PATH', APPPATH . 'logs/plurk_log');
3. 新增 Common.php 跟 Plurk.php 檔案到 application/libraries/ 資料夾裡面 Common.php 檔案如下: http://github.com/appleboy/CodeIgniter-Plurk-API/blob/master/libraries/Common.php Plurk.php 主程式: http://github.com/appleboy/CodeIgniter-Plurk-API/blob/master/libraries/Plurk.php 4. 接下來就可以新增 Controller 來測試看看,直接些改 CodeIgniter 所預設的 Welcome Controller
config->load('plurk_config');
        $this->load->library('plurk');

	}
	
	function index()
	{ 
        $api_key = $this->config->item('api_key');
        $username = $this->config->item('username');
        $password = $this->config->item('password');
        $this->plurk->login($api_key, $username, $password);  
        
        /**
         ******************************************
         * @Get plurks
         *
         * set plurk id = {123, 456, 789}
         ******************************************/
        
        echo "

----- get plurks -----

"; echo "
";
        print_r($this->plurk->get_plurks());
        echo "
"; /* echo "

----- get someone's plurk -----

"; print_r($plurk->get_plurk(123)); echo "

----- get unread plurks -----

"; print_r($plurk->get_unread_plurks()); echo "

----- mark plurk as read -----

"; $plurk->mark_plurk_as_read(array(123,456,789)); echo "

----- add plurk -----

"; $plurk->add_plurk('en', 'says', 'Hello World'); echo "

----- edit plurk -----

"; $plurk->edit_plurk(123, 'be edited'); echo "

----- delete plurk -----

"; $plurk->delete_plurk(123); echo "

----- mute plurks -----

"; print_r($plurk->mute_plurks(123)); echo "

----- unmute plurks -----

"; print_r($plurk->unmute_plurks(123)); */ /** ****************************************** * @Get alerts * ******************************************/ /* echo "

----- get active alerts -----

"; print_r($plurk->get_active()); echo "

----- get a list of past 30 alerts -----

"; print_r($plurk->get_history()); echo "

----- remove notification -----

"; $plurk->remove_notification(123); */ /** ****************************************** * @Get plurk's responses * ******************************************/ /* echo "

----- get responses -----

"; echo "set plurk id = 123"; print_r($plurk->get_responses(123)); echo "

----- add response -----

"; echo "set plurk id = 123"; print_r($plurk->add_response(123, 'test response', 'says')); echo "

----- delete response -----

"; echo "set plurk id = 123, response id = 456"; $plurk->delete_response(123, 456); */ /** ****************************************** * @Control user * ******************************************/ /* echo "

----- get own profile -----

"; print_r($plurk->get_own_profile()); echo "

----- get user public profile -----

"; echo "set user id = 123"; print_r($plurk->get_public_profile(123)); echo "

----- get user info -----

"; print_r($plurk->get_user_info()); echo "

------ get users friends (nick name and full name)

"; print_r($plurk->get_completion()); echo "

----- get block user's list -----

"; print_r($plurk->get_blocks()); echo "

----- block user -----

"; $plurk->block_user(5366984); echo "

----- unblock user -----

"; $plurk->unblock_user(5366984); */ /** ****************************************** * @Control friends * * set user id = 123 * set friend id = 789 ******************************************/ /* echo "

----- get someone's friends -----

"; print_r($plurk->get_friends(123)); echo "

----- become someone's friend -----

"; $plurk->become_friend(789); echo "

----- remove friend -----

"; $plurk->remove_friend(789); echo "

----- accept friendship request as friend -----

"; $plurk->add_as_friend(789); echo "

----- accept all friendship requests as friends -----

"; $plurk->add_all_as_friends(); echo "

----- deny friendship -----

"; $plurk->deny_friendship(789); */ /* ****************************************** * @Control fans * * set user id = 123 * set fan id = 789 ******************************************/ /* echo "

----- get following -----

"; print_r($plurk->get_following()); echo "

----- get someone's fans -----

"; print_r($plurk->get_fans(123)); echo "

----- become someone's fan -----

"; $plurk->become_fan(5366983); echo "

----- accept a friendship request as fan -----

"; plurk->add_as_fan(789); echo "

----- accept all friendship requests as fans -----

"; $plurk->add_all_as_fan(); */ /* can't use */ //echo "

----- set user following -----

"; //echo "user id = 789"; //echo ($plurk->set_following(3440147, $follow = FALSE)) ? 'success' : 'disable'; /* ****************************************** * @Search * ******************************************/ /* echo "

----- search plurk -----

"; print_r($plurk->search_plurk('php-plurk-api')); echo "

----- search user -----

"; print_r($plurk->search_user('roga lin')); echo "

----- get emoticons -----

"; print_r($plurk->get_emoticons()); */ /* ****************************************** * @Clique * ******************************************/ /* echo "

----- get clique list -----

"; print_r($plurk->get_cliques()); echo "

----- create a clique -----

"; print_r($plurk->create_clique("test")); echo "

----- rename clique -----

"; print_r($plurk->rename_clique("test","test1")); echo "

----- get clique -----

"; print_r($plurk->get_clique('test1')); echo "

----- add a user to a clique -----

"; print_r($plurk->add_to_clique("test1", 3440147)); echo "

----- remove a user from a clique -----

"; print_r($plurk->remove_from_clique("test1", 3440147)); echo "

----- delete a clique -----

"; print_r($plurk->delete_clique("test1")); */ } } /* End of file welcome.php */ /* Location: ./system/application/controllers/welcome.php */
底下載入相關設定以及 Plurk Library,當然也可以設定在 application/config/autoload.php 裡面喔
$this->config->load('plurk_config');
$this->load->library('plurk');
這樣就算是安裝成功了,大家可以試試看,如果需要程式碼,可以到 CodeIgniter-Plurk-API 下載完整的程式碼

INSTALLATION

Download all file from this site.
$ http://github.com/appleboy/CodeIgniter-Plurk-API/archives/master
$ git clone git://github.com/appleboy/CodeIgniter-Plurk-API.git
Copy some files into directory.
$ copy config/plurk_config.php your_application/config/
$ copy config/plurk_constant.php your_application/config/
$ copy libraries/Common.php your_application/libraries/
$ copy libraries/Plurk.php your_application/libraries/
Create logs directory, and chmod 755 directory
$ mkdir your_application/logs
$ chmod 755 your_application/logs
$ chown www:www your_application/logs
Edit config/plurk_config.php, and configure your api key, plurk username, and plurk password
$config['api_key'] = "xxxxx";
$config['username'] = "xxxxx";
$config['password'] = "xxxxx";
Test your controller file: welcome.php http://localhost/welcome