【发布时间】:2012-08-08 17:31:37
【问题描述】:
我很难为这个问题找到一个好的标题,所以我希望这很清楚。我目前在我的一个网站上使用 TwitterOauth 模块来发布推文。虽然这可行,但我需要限制提交的推文数量; 每小时只有一次。
注意:我没有使用数据库的选项。这对于这个问题至关重要。
我已将其合并到处理实际发布到 Twitter API 的 PHP 文件中,如下所示:
# Save the timestamp, make sure lastSentTweet exists and is writeable
function saveTimestamp(){
$myFile = "./lastSentTweet.inc";
$fh = fopen($myFile, 'w');
$stringData = '<?php function getLastTweetTimestamp() { return '.time().';}';
fwrite($fh, $stringData);
fclose($fh);
}
# Include the lastSentTweet time
include('./lastSentTweet.inc');
# Define the delay
define('TWEET_DELAY', 3600);
# Check for the last tweet
if (time() > getLastTweetTimestamp() + TWEET_DELAY) {
// Posting to Twitter API here
} else {
die("No.");
}
lastSentTweet.inc 文件(chmod 777)的(初始)内容:
<?php function getLastTweetTimestamp() { return 1344362207;}
问题是,虽然这有效;它允许意外的双重提交;如果多个用户(并且运行此脚本的站点当前非常繁忙)触发此脚本,则恰好有 2 次提交(或更多,尽管尚未发生)到 Twitter 滑过,而不仅仅是 1。我的第一个想法是打开、写入和关闭文件的延迟(虽然是一分钟),但我可能是错的。
有没有人知道什么允许意外的双重提交(以及如何解决这个问题)?
【问题讨论】:
-
对不起,忘了提:我不能(将其添加到问题中)。否则我当然会;)
-
无法想象你不能使用数据库的原因。 php内置了一个
-
但这不会给我同样的问题吗?
标签: php twitter submit twitter-oauth fwrite