Creating Plurk Bot Using PHP
We know each users in Plurk have a Karma score. Karma score depending your activity on Plurk. If we are active then the value will be increase, vice versa.
Oke now I will show you how to create Plurk bot using PHP so your Karma will not decrease.
What are required to create a bot? First, You need server to host the PHP script. If you don’t have, You can place the script on free hosting. To make the script executed, You can use Cron job on your hosting. Or We can use free cron using image tag alternatively, that embeded in forum or mailing list.
PHP is required to execute the script. The script contain routine from getting latest news from RSS web site until send a plurk content to Plurk web service.
The content for sample taken from BBC.co.uk RSS feed and take one of items randomly.
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | $apiKey = 'YOUR API KEY'; $username = 'USER THAT YOU WANT TO LOGIN'; $password = 'USER PASSWORD'; /** * @author syabac http://blog.syabac.web.id */ class Plurk { private $_apiUrl = 'http://www.plurk.com/API/'; private $_apiKey; private $_username; private $_password; public function __construct($apiKey, $user, $pass){ $this->_apiKey = $apiKey; $this->_username = $user; $this->_password = $pass; } private function _buildUrl($method, array $params){ return $this->_apiUrl . $method . '?' . http_build_query($params); } private function _sendRequest($method, array $params){ $url = $this->_buildUrl($method, $params); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt_array($curl, array( CURLOPT_CONNECTTIMEOUT => 0, CURLOPT_COOKIEJAR => 'cookies.plurk', CURLOPT_COOKIEFILE => 'cookies.plurk' )); $response = curl_exec($curl); curl_close($curl); return $response; } public function login(){ return $this->_sendRequest('Users/login', array( 'api_key' => $this->_apiKey, 'username' => $this->_username, 'password' => $this->_password ) ); } public function logout(){ return $this->_sendRequest('Users/logout', array( 'api_key' => $this->_apiKey ) ); } public function addPlurk($plurk){ return $this->_sendRequest('Timeline/plurkAdd', array( 'api_key' => $this->_apiKey, 'content' => $plurk, 'qualifier' => 'says', 'lang' => 'id' ) ); } } class RandomPlurk { public static function generate(){ libxml_use_internal_errors(true); //You may change the RSS url by editing this line. $rss = simplexml_load_file('http://newsrss.bbc.co.uk/rss/sportonline_world_edition/front_page/rss.xml'); $items = $rss->channel->item; $item = $items[mt_rand(0, count($items)-1)]; return sprintf("%s (%s)", $item->link, $item->title); } } function isSend($delay) { if(! file_exists('last_update.plurk')){ file_put_contents('last_update.plurk', time()); return true; } $lastUpdate = file_get_contents('last_update.plurk'); $delay = 60 * 60 * $delay; $now = time(); if( $now - $lastUpdate > $delay){ file_put_contents('last_update.plurk', $now); return true; } return false; } $delay = 4; //in HOUR if(isSend($delay)){ $status = RandomPlurk::generate(); $plurk = new Plurk($apiKey, $username, $password); $plurk->login(); $plurk->addPlurk($status); $plurk->logout(); } |
Save the script and set file name plurk.php. Upload the script to your accessible folder from web on your server, for instance the document root folder. So that the file can be accessed by accessing http://example.com/plurk.php URL.
To trigger the script to execute program, we have to make the URL requested by users from web. We can use HTML image tag (img) to trigger the sript. The tag may look like this:
<img src=”http://example.com/plurk.php” />
Or if you member of forum, you can embed the BBCode on your post.
[img]http://example.com/plurk.php[/img]
So, the script will be triggered by users that access pages that are embeded your HTML image tag. Done. You have created simple bot using PHP. You just wait other users access your page then tadaaa, your Plurk updated.
Have fun.







