On my last post about Plurk, I write about Plurk’s API. Now, as my promise I want to show you an easy way to use them. Please note, I will only use php script. Because I eat from this script. First, you can download the plurk’s API script and get API code by accessing this URL. Here we go.
After you download the script, extract script. You will get a complete example scripts and documentation. Please note: to make a bot through plurk, always include three scripts, namely common.php, config.php, and plurk_api.php. Without all three you will get anything. Then request the API code in plurk. Once you can fire it, let’s get to coding.
The basic of all coding on plurk is like this, you can look at example.php:
#!/usr/bin/php5
<?php
/**
* @desc: Get the API key via Official API website, http://www.plurk.com/API
*
*/
$api_key = 'YourAPIkey';
$username = 'yournamehere';
$password = 'yourpasswordhere';
require('plurk_API/plurk_api.php');
$plurk = new plurk_api();
$plurk->login($api_key, $username, $password);
$api_key is define your API key, $username fill with your username and $password fill with your password plurk. Off course to use this you must registered. Then you should instance the object plurk_api, then use login. Very simple. Copy paste that code to your bot script. You can append a connection script to your database. It will usefull later.
Using bot, I use cron job. There is so many advantage to use cron job as bot. There is another way to use bot, without using cron, but surely the simple and easy way is by using this method. With cron, you can call what script you want to run at the given time. Example we want to run the bot about quote. After the above configuration, we now create a function to retrieve content, such as we draw from a database. The scenario is we’ll fill plurk quote-quote once every 4 hours for example. So I took the quotes assumptions are taken from our database. For example we create a table containing the quote id, content, is_plurked. Composition like this:
CREATE TABLE quote( id int primary key, content text, is_plurked tinyint default 0);
After creating table, try to fill it :
insert into quote (content) values ('To to to be or not to to to be, that is the question'), ('Hello this is my plurk bot')
Then, we create function to call it.
function getContent()
{
global $conn;
$content = "";
$sql = "SELECT id, `content` FROM `quote` WHERE is_plurked = 0 ORDER BY RAND() LIMIT 1";
$rs = mysql_query($sql,$conn) or die(mysql_error());
if (false !== $r = mysql_fetch_array($rs))
{
$content = $r['content'];
$id = $r['id'];
}
return array($id, $content);
}
Now about function to send plurk
function sendPlurk($array)
{
global $conn,$plurk;
list($id, $content) = $array;
$newcontent = explode("\n", wordwrap($content, 135, "\n"));
foreach($newcontent as $key => $value)
{
if ($key == 0)
{
$plurk->add_plurk('en', ':', $value);
writelog("sending $value\n");
}
else
{
$plurks = $plurk->get_plurks('123'); // << 123 is your plurk_id
$plurk_id = $plurks->plurks[0]->plurk_id;
$plurk->add_response($plurk_id, $value);
writelog("sending $value on plurkid $plurk_id\n");
}
}
$sql = "UPDATE quote SET is_plurked = 1 WHERE id = '$id'";
mysql_query($sql,$conn) or die(mysql_error());
writelog("sending one session done!\n");
}
As you can see the getContent function above, I use the return value as an array. This is because later I will run the function with the results sendPlurk return of function getContent, so I will immediately get the two variables id and content. Then, because of the limitations plurk that only 140 characters, so with wordwrap function 140 separates the characters into new lines.
If you see above, I have written writelog. Actually, it also does not use too fine, but this writelog important because you never know what can happen when cron is running, may be we’re not at the computer. This log which will tell us whether the process is running smoothly or not. You can just remove it, but I still wear them.
Well if you use writelog, the script is more like this:
function writelog($log)
{
$file = "log.bot";
$fh = fopen($file, 'ab+');
fwrite($fh, $log);
fclose($fh);
}
Just make sure you have open permission to read and write! If not, its useless. Then to main code is, could be like this:
<?php
//declaration plurk
//login plurk
//connection
$content = getContent();
sendPlurk($content);
writelog("sending done\n");
?>
The last thing! You should configure the crontab. Edit the file /etc/crontab. More how to at wikipedia. Set the crontab like this (its depends on your linux OS File System, the location of binary php-cli could be different):
2 */4 * * * root /usr/bin/php /var/www/htdocs/plurk/bot_quote.php
Yep, its done. Try it and Good luck!








tutorialnya bagus mas..
lagi dong..
tapi jujur masih blum ngeh yg ini
[...] I’ve always wanted to create a bot for automated postings to Plurk just to get my karma rising, but I never got to coding it. Besides, that isn’t exactly acceptable behavior as some would see it. But here’s a complete guide to creating your own plurk bot using PHP. [...]