自從在我舊的 blog 上發表了一些有關 plurk 上噗文的方法,
受到一些朋友的意見...希望知道如何用php來發噗~~~
因此,
將我一年前的程式(寫得不太好)公開獻醜, 如下:
受到一些朋友的意見...希望知道如何用php來發噗~~~
因此,
將我一年前的程式(寫得不太好)公開獻醜, 如下:
<?php //設置編碼為 UTF-8 header('Content-Type:text/html; charset=UTF-8'); function timerloop() { $mtime = microtime(); $mtime = explode(' ', $mtime); $mtime = $mtime[1] + $mtime[0]; $rtime = $mtime; return $rtime; } function timer($starttime) { $endtime = timerloop(); $totaltime = ($endtime - $starttime); while ($totaltime < 4) { $endtime = timerloop(); $totaltime = ($endtime - $starttime); } } //Plurk 的暱稱 define('NICKNAME', 'your nickname'); //Plurk 的密碼 define('PASSWORD', 'your password'); //Plurk 的系統編號, 在你的 Plurk 頁面檢視網頁原始碼, 找 "user_id": define('USER_ID', 'your user id'); //Plurk qualifier: default is 'says' define('DQUALIFIER', 'says'); define('QUALIFIER', 'shares'); //RSS來源網址列表陣列 $rssfeed = array("http://tw.news.yahoo.com/rss/tech"); //Yahoo!奇摩新聞-科技 //分解開始 for($i = 0; $i < sizeof($rssfeed); $i++) { $buff = ""; //打開RSS網址並讀取, 讀取失敗則中止 $fp = fopen($rssfeed[$i], "r") or die("can not open $rssfeed"); while ( !feof($fp) ) { $buff .= fgets($fp, 4096); } //關閉文件打開 fclose($fp); $rss_str = ""; //建立一個 XML 解析器 $parser = xml_parser_create(); //xml_parser_set_option -- 為指定 XML 解析進行選項設置 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); //xml_parse_into_struct -- 將 XML 數據解析到數組 $values 中 xml_parse_into_struct($parser, $buff, $values, $idx); //xml_parser_free -- 釋放指定的 XML 解析器 xml_parser_free($parser); foreach ($values as $val) { $tag = $val["tag"]; $type = $val["type"]; $value = $val["value"]; //標籤統一轉為小寫 $tag = strtolower($tag); if ($tag == "item" && $type == "open") { $is_item = 1; }else if ($tag == "item" && $type == "close") { //構造輸出字符串 $rss_str .= $link." (".$title.") \r\n`"; $is_item = 0; } //僅讀取- 標籤中的內容 if($is_item == 1) { if ($tag == "title") { $title1 = str_replace("(", "[", $value); $title2 = str_replace(")", "]", $title1); $title = $title2; } if ($tag == "link") { $link = $value; } } } } if($rss_str != "") { //Plurk message $message = "**Yahoo!奇摩科技新聞** __".date("Y/m/d H:i:s", time() + 8 * 3600)."__ *如下* http://l.yimg.com/tw.yimg.com/i/tw/hp/masthead/mhlogo.png"; $curl_handle = curl_init(); curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl_handle, CURLOPT_COOKIEJAR, 'cookie.txt'); curl_setopt($curl_handle, CURLOPT_COOKIEFILE, 'cookie.txt'); //Plurk login curl_setopt($curl_handle, CURLOPT_URL, 'http://www.plurk.com/Users/login'); curl_setopt($curl_handle, CURLOPT_POSTFIELDS, 'nick_name='.NICKNAME.'&password='.PASSWORD); curl_exec($curl_handle); //Plurk post curl_setopt($curl_handle, CURLOPT_URL, 'http://www.plurk.com/TimeLine/addPlurk'); curl_setopt($curl_handle, CURLOPT_POSTFIELDS, 'qualifier='.QUALIFIER.'&content='.urlencode($message).'&lang=tr_ch&uid='.USER_ID.'&no_comments=0'); curl_exec($curl_handle); //Get plurk_id, to find the string="plurk_id": curl_setopt($curl_handle, CURLOPT_URL, 'http://www.plurk.com/TimeLine/getPlurks'); curl_setopt($curl_handle, CURLOPT_POSTFIELDS, 'user_id='.USER_ID); $cnt = curl_exec($curl_handle); //To find the first of plurk_id $cnt1 = strstr($cnt, '"plurk_id": '); $cnt2 = substr($cnt1, 0, strpos($cnt1, ',')); $cnt3 = substr($cnt2, strpos($cnt1, ' ')); $plurk_id = ltrim($cnt3); //remove the last symbol "`" $rss_str = substr($rss_str, 0, strlen($rss_str) - 1); $rec = 0; $tok = explode("`", $rss_str); foreach($tok as $value2print) { $rmessage = $value2print; //Plurk response curl_setopt($curl_handle, CURLOPT_URL, 'http://www.plurk.com/Responses/add'); curl_setopt($curl_handle, CURLOPT_POSTFIELDS, 'qualifier='.QUALIFIER.'&content='.urlencode($rmessage).'&p_uid='.USER_ID.'&plurk_id='.$plurk_id.'&lang=tr_ch&uid='.USER_ID); curl_exec($curl_handle); $rec++; $starttime = timerloop(); timer($starttime); } //total records $rmessage = iconv("BIG5", "UTF-8", "**A total of $rec records.**"); curl_setopt($curl_handle, CURLOPT_URL, 'http://www.plurk.com/Responses/add'); curl_setopt($curl_handle, CURLOPT_POSTFIELDS, 'qualifier='.QUALIFIER.'&content='.urlencode($rmessage).'&p_uid='.USER_ID.'&plurk_id='.$plurk_id.'&lang=tr_ch&uid='.USER_ID); curl_exec($curl_handle); curl_close($curl_handle); } exit(0); ?>
如果上述php程式懶得打...
留言