Niels' Blog

A Dutch techie in China

Archive for the ‘Web’ Category

Syncing Fanfou with Twitter

@kofai asked me to share my Fanfou to Twitter sync script. It’s not art, but it works, so here goes:

<?php
define(STATUS_PATH, '/home/www/twitbridg/status/');

include_once('password.php');
$since_id = file_get_contents(STATUS_PATH.$twitter_username.'.last') OR $since_id = '1';
// The twitter API address
$url = 'https://twitter.com/statuses/user_timeline/'.$twitter_username.'.xml?since_id='.$since_id;

$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_GET, 1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

if (empty($buffer))
  die('failed to get tweets: '.$since_id."\n");
$xml = new SimpleXMLElement($buffer);
$i = sizeof($xml->status)-1;

if($i<0) exit;

while($i >= 0) {
// Update FanFou
$url = 'http://api.fanfou.com/statuses/update.xml';
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "source=".urlencode("<a href=\"http://twitter.com/".$twitter_username."\">Twitter</a>")."&amp;status=".urlencode($xml->status[$i]->text));
curl_setopt($curl_handle, CURLOPT_USERPWD, "$fanfou_username:$fanfou_password");
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
$i--;
}
$last_id = $xml->status[0]->id;
$fp = fopen(STATUS_PATH.$twitter_username.'.last', 'w');
fwrite($fp, $last_id);
fclose($fp);
?>

As you can see it includes a password.php. This sets a few login variables. You can just insert them at the top of the above PHP file, but as I edit my code in public places I prefer not to do that.

<?php
$twitter_username = 'nielspeen';
$fanfou_username = 'nielspeen';
$fanfou_password = '123xyz';
?>

Enjoy!

  • 3 Comments
  • Filed under: Tech, Web
  • AliPay on GNU/Linux

    AliPay is the Paypal of China and hard to ignore if you want to use websites like TaoBao (the Chinese eBay.) Unlike Paypal however, it requires ActiveX components or Firefox plugins to function. Getting this to work on Linux can be a pain if you don’t read Chinese. So here’s in English:

    Download http://blog.alipay.com/wp-content/2008/10/aliedit.tar.gz

    Extract it into /usr/lib/mozilla/plugins (as root) or into ~/.mozilla/plugins (if you care about the current user only.) Restart Firefox and things should work! (Select Tools, Add-ons, Plugins to verify that Aliedit has been installed.)

    Plugins directory may vary on other distributions.

  • 0 Comments
  • Filed under: China, Tech, Web