我有一個每兩分鐘運行一次「Tweet-getter」應用程序的腳本。簡而言之,它會在Facebook上發佈推文。時不時地打嗝,儘管我的錯誤檢查,每隔兩分鐘(它作爲一個cron工作運行的週期)不斷重播舊的推文。我有一個log.txt,理論上可以幫助我確定這裏發生了什麼,但問題在於它不會在每次作業運行時寫入。下面的代碼:。用PHP編寫的不可預測的日誌文件
<?php
$start_time = microtime();
require_once //a library and config
$facebook = new Facebook($api_key, $secret);
get_db_conn(); //returns $conn
$hold_me = mysql_fetch_array(mysql_query("SELECT * FROM `stats`"));
$last_id_posted = $hold_me[0]; //the status # of the most recently posted tweet
$me = "mytwittername";
$ch = curl_init("http://twitter.com/statuses/friends_timeline.xml?since_id=$last_id_posted");
curl_setopt($ch, CURLOPT_USERPWD, $me.":".$pw);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xs = curl_exec($ch);
$data = new SimpleXMLElement($xs);
$latest_tweet_id = $last_id_posted;
$uid = get_uid(); //returns an array of facebookID->twittername
$user_count = count($uid);
curl_close($ch);
$total_tweets = 0;
$posted_tweets = 0;
foreach ($data->status as $tweet) {
$name = strtolower($tweet->user->screen_name);
if (array_key_exists($name, $uid)) {
$total_tweets += 1;
// $name = Twitter Name
$message = $tweet->text;
$fbid = $uid[$name];
theposting($name,$message,$fbid); //posts tweet to facebook
$this_id = $tweet->id;
if ($this_id > $latest_tweet_id) {
$latest_tweet_id = $this_id;
}
}
}
mysql_query("UPDATE stats SET lasttweet='$latest_tweet_id'");
commit_log(); //logs to a txt file how many tweets posted, how many users, execution duration, and time of execution
?>
所以理論上日誌是「八月24日星期一2009年下午10時41分32秒召集所有自#3326415954.更新至#8 3526415953.用戶字符串接過0.086057毫秒在20條推文中發佈了14條。「線。有時候,它會一次跳過兩三個小時,在那段時間內,它會用同一推文的多個副本「垃圾」人們的臉書頁面。我不知道什麼可能會破壞我的代碼,但我的懷疑是來自twitter的糟糕的XML。總而言之,它的流量相對較低,所以我懷疑我的服務器負載過重或其他問題。 log.txt現在是50kb,最後「破」在〜35kb,所以它不是一個巨大的文件放慢速度...任何想法,將不勝感激!