2009年9月6日星期日

PLURK BOT 範例

這是一個以 自由時報即時新聞 為基礎建立的 php plurk bot 範例
會貼出最新的10條新聞

注意
要把程式內的 12345 及 67890 替換為你 PLURK 的登入帳號及密碼
且須用到 cURL 模組
請確認你的環境是否有支援。

  1. <?
  2. header('Content-type:text/html; charset=utf-8');
  3. define('NICKNAME', '12345'); //12345 => 改為你的 PLURK ID
  4. define('PASSWORD', '67890'); //67890 => 改為你的 PLURK PASSWORD
  5. define('USER_ID', getuserid(NICKNAME));
  6.  
  7. // login plurk ===========================================================
  8.     $ch = curl_init();
  9.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  10.     curl_setopt($ch, CURLOPT_COOKIEJAR, 'plurk_cookie.txt');
  11.     curl_setopt($ch, CURLOPT_URL, 'http://www.plurk.com/Users/login');
  12.     curl_setopt($ch, CURLOPT_POSTFIELDS, 'nick_name='.NICKNAME.'&password='.PASSWORD);
  13.     curl_exec($ch);
  14.     curl_close($ch);
  15. //=========================================================================  
  16.  
  17. $message = '**Libertytimes** Live News';
  18. plurk($message);
  19.  
  20. $url = "http://iservice.libertytimes.com.tw/liveNews/list2.php";
  21. $baseurl = "http://iservice.libertytimes.com.tw/liveNews/";
  22. $contents = download($url);
  23.  
  24. preg_match_all('/<li>(.*?)<\/li>/s',$contents,$match);
  25. if ($match[0]){
  26.     $plurkid = getplurkid();
  27.     for ($i=0;$i<10;$i++){
  28.         sleep(5);
  29.         $out = $match[0][$i];
  30.         $link = tinyurl(preg_replace('/(.*?)<a href=\"(.*?)\"(.*)/s',"$baseurl$2",$out));
  31.         $out = preg_replace('/(.*?)<a href=\"(.*?)\" target=_blank>(.*?)<\/a><\/span>(.*?)<\/li>/s',"$link ($3) $4",$out);
  32.         replurk($out,$plurkid);  
  33.     }
  34. }
  35.  
  36. function download($url){
  37.     $ch = curl_init();
  38.     curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)');
  39.     curl_setopt($ch,CURLOPT_URL,$url);
  40.     curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  41.     $contents = curl_exec($ch);
  42.     curl_close($ch);
  43.     return $contents;
  44. }
  45.  
  46. function getuserid(){
  47.     $url= 'http://www.plurk.com/'.NICKNAME;
  48.     $fid = download($url);
  49.     preg_match('/user_id\": (.*?)\,/s',$fid,$matches);
  50.     $u_id = $matches[1];
  51.     return($u_id);
  52. }
  53.  
  54. function tinyurl($url){
  55.     return(trim(download('http://tinyurl.com/api-create.php?url='.$url)));
  56. }
  57.  
  58. function plurk($message){
  59.   // plurk
  60.     $ch = curl_init();
  61.     curl_setopt($ch, CURLOPT_COOKIEFILE, 'plurk_cookie.txt');
  62.     curl_setopt($ch, CURLOPT_URL, 'http://www.plurk.com/TimeLine/addPlurk');
  63.     curl_setopt($ch, CURLOPT_POSTFIELDS, 'qualifier=gives&content='.urlencode($message).'&lang=tr_ch&no_comments=0&uid='.USER_ID);
  64.     curl_exec($ch);
  65.     curl_close($ch);
  66. }
  67.  
  68. function replurk($message,$plurk_id){
  69.   // replurk
  70.   $ch = curl_init();
  71.   curl_setopt($ch, CURLOPT_COOKIEFILE, 'plurk_cookie.txt');
  72.   curl_setopt($ch, CURLOPT_URL, 'http://www.plurk.com/Responses/add');
  73.   curl_setopt($ch, CURLOPT_POSTFIELDS, 'posted='.$update.'&qualifier=gives&content='.urlencode($message).'&p_uid='.USER_ID.'&plurk_id='.$plurk_id.'&lang=tr_ch&no_comments=0&uid='.USER_ID);
  74.   curl_exec($ch);
  75.   curl_close($ch);
  76. } 
  77.  
  78. function getplurkid(){
  79. //get plurk id
  80.     sleep(2);
  81.     $url   = 'http://www.plurk.com/'.NICKNAME.'.xml';
  82.     $plurk = download($url);
  83.     preg_match('/<id>tag:plurk.com\;(.*?)\;(.*?)</s',$plurk,$pid);
  84.     $plurk_id = $pid[2];
  85.     return($plurk_id);
  86. }
  87. ?>


結果如下:



當然,你可再搭配排程軟體定時發佈新聞。


.

9 留言回覆::

  1. 我是使用appserv-win32-2.5.10 + 您介紹啟動cURL模組,但會出現亂碼ㄟ!!!

    回覆刪除
  2. @Hung
    我不知問題出在哪
    可否告知些 亂碼 的樣本
    謝謝。

    回覆刪除
  3. 下面就是我得到的亂碼

    {"plurk": {"lang": "tr_ch", "content": "**Libertytimes**<\/b> Live News", "content_raw": "**Libertytimes** Live News", "user_id": 4722732, "plurk_type": 0, "plurk_id": 116694091, "response_count": 0, "qualifier": "gives", "id": 116694091, "is_unread": 0, "responses_seen": 0, "posted": new Date("Mon, 14 Sep 2009 15:33:22 GMT"), "limited_to": null, "no_comments": 0, "owner_id": 4722732}, "error": null}{"object": {"lang": "tr_ch", "content_raw": "http:\/\/tinyurl.com\/lxl823 (\u5fb7\u7e3d\u7406\u7af6\u9078\u96fb\u8996\u8faf\u8ad6\u5f8c \u53f2\u5766\u9ea5\u723e\u6c23\u52e2\u5927\u632f) 09-14 23:25", "user_id": 4722732, "qualifier": "gives", "plurk_id": 116694091, "content

    回覆刪除
  4. @Hung
    上面的亂碼是 plurk server 回應的,不用理它。
    其實你發的新聞噗有成功發出。

    http://www.plurk.com/p/1xh5qj

    回覆刪除
  5. 我執行plurk bot後,出現一片白色畫面.....

    不知為何?

    回覆刪除
  6. 感謝您的分享大作,我也測試成功可用,耶!!!不過我試了同時私噗給兩人都不成功,是不是語法錯誤?limited_to後面如何接多個人呢??向您求救了!

    回覆刪除
  7. @柴魁客漫遊
    假設要傳給兩個人私噗
    你的 uid 是 77777
    他們的 uid 分別是 12345 及 67890
    那 limited_to= 後面要接的是
    [77777,"12345","67890"] urlencode 後的值
    也就是
    limited_to=%5B77777%2C%2212345%22%2C%2267890%22%5D

    回覆刪除
  8. 收到,非常感謝!我立即去Try....

    回覆刪除
  9. 網誌管理員已經移除這則留言。

    回覆刪除