如何在 PHP 中安排/排队内容以便将来发布?

发布于 2024-10-06 03:32:59 字数 2148 浏览 0 评论 0原文

我有一个非常基本的 PHP 脚本,我用它来发布有趣的链接,我发现这些链接到我网站上的可过滤列表以及我的 rss feed(feedburner 在 ping 时也会发送推文)。

我想知道的是,添加一个“队列”,让我可以一次提交多个条目并为每个条目安排未来的发布时间/日期,会有多困难?

类似于 Twuffer 对 Twitter 或 Tumblr 和 Wordpress 已经为博客文章做好了准备。

这需要 cron 作业吗?也许我的 PHP 脚本会写入另一个文件'drafts.txt'(如果是未来的帖子) - 以及计划的 cron 来检查如果时间/日期=,然后将其写入其他文件?

我显然是这方面的新手 - 但我将不胜感激任何帮助!谢谢!

这是我当前的小脚本:

<?php 

if($_POST['Submit']) 
{ 
$category = $_POST['category']; 
$linkurl = $_POST['linkurl']; 
$linkname = $_POST['linkname']; 
$description = $_POST['description']; 
$submittername = $_POST['submittername']; 
$submitterurl = $_POST['submitterurl']; 
$postdate = $_POST['postdate'];

// Remove slashes.
$description = stripslashes($description);

//the data for list.txt
$data = "
<li class='$category'>
    <h3><a href='$linkurl' target='_blank'>$linkname</a></h3>
    <p><b>$description</b></p> 
    <p><small>Submitted by: <a href='$submitterurl' target='_blank'>$submittername</a><i> - $postdate</i></small></p>
</li>
"; 

$filename   = "list.txt";

$fp         = fopen( $filename,"r"); 
$OldData    = fread($fp, 80000); 
fclose( $fp ); 

$New = "$data$OldData";

$fp = fopen( $filename,"w"); 
if(!$fp) die("Cannot write $filename .");
fwrite($fp, $New, 800000); 
fclose( $fp ); 

//the data for rss.php
$feeddata = "
<item>
   <title>Supplement: $linkname</title>
   <link>$linkurl</link>
   <description>$description</description>
</item>
"; 

$ffilename  = "rss.txt";

$ff         = fopen( $ffilename,"r"); 
$OldfeedData    = fread($ff, 80000); 
fclose( $ff ); 

$New = "$feeddata$OldfeedData";

$ff = fopen( $ffilename,"w"); 
if(!$ff) die("Cannot write $ffilename .");
fwrite($ff, $New, 800000); 
fclose( $ff ); 


print("<h1>Success!</h1><a href='add.php'>Add Another?</a>");
}
?>

I have a very basic PHP script that I use to post interesting links that I find to a filterable list on my site and also to my rss feed (which feedburner then also tweets when pinged).

What I'm wondering is how hard it would be to add a 'queue' in which I could submit multiple entries at once and schedule a future time/date for each to be released?

Similar to what Twuffer does for Twitter or Tumblr and Wordpress have done for Blog Posts.

Does this require cron jobs? Perhaps with my PHP script writing another file 'drafts.txt' if it's a future post - and a scheduled cron to check if time/date =, then write it to the other files?

I'm obviously a newbie with this - but I would appreciate any help! Thanks!

Here is my current little script:

<?php 

if($_POST['Submit']) 
{ 
$category = $_POST['category']; 
$linkurl = $_POST['linkurl']; 
$linkname = $_POST['linkname']; 
$description = $_POST['description']; 
$submittername = $_POST['submittername']; 
$submitterurl = $_POST['submitterurl']; 
$postdate = $_POST['postdate'];

// Remove slashes.
$description = stripslashes($description);

//the data for list.txt
$data = "
<li class='$category'>
    <h3><a href='$linkurl' target='_blank'>$linkname</a></h3>
    <p><b>$description</b></p> 
    <p><small>Submitted by: <a href='$submitterurl' target='_blank'>$submittername</a><i> - $postdate</i></small></p>
</li>
"; 

$filename   = "list.txt";

$fp         = fopen( $filename,"r"); 
$OldData    = fread($fp, 80000); 
fclose( $fp ); 

$New = "$data$OldData";

$fp = fopen( $filename,"w"); 
if(!$fp) die("Cannot write $filename .");
fwrite($fp, $New, 800000); 
fclose( $fp ); 

//the data for rss.php
$feeddata = "
<item>
   <title>Supplement: $linkname</title>
   <link>$linkurl</link>
   <description>$description</description>
</item>
"; 

$ffilename  = "rss.txt";

$ff         = fopen( $ffilename,"r"); 
$OldfeedData    = fread($ff, 80000); 
fclose( $ff ); 

$New = "$feeddata$OldfeedData";

$ff = fopen( $ffilename,"w"); 
if(!$ff) die("Cannot write $ffilename .");
fwrite($ff, $New, 800000); 
fclose( $ff ); 


print("<h1>Success!</h1><a href='add.php'>Add Another?</a>");
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里人 2024-10-13 03:32:59

这就是我最终所做的(感谢阿尔弗雷德让我朝着正确的方向思考):

  • 我在表单中添加了一个单选按钮来发布“现在”或“稍后”。
  • 选择“稍后”会显示一个日期选择器,它将日期时间变量传递到我的脚本中,以供我希望何时发布文章。
  • 在我的脚本中,如果选择“Now”,数据将直接写入 .txt 文件,如上所示。如果选择“稍后”,数据将写入(带有发布时间戳)到 MySQL 数据库。
  • 我安排了一个 cron 作业,每十分钟运行另一个 php 脚本,以写入带有已传递到我的 .txt 文件的时间戳的任何数据,并从数据库中删除该条目。

效果很好!

Here's what I ended up doing (thanks to Alfred for getting me thinking in the right direction):

  • I added a radio buttons in my form to post "Now" or "Later".
  • Selecting "Later" reveals a datepicker which passes a datetime var into my script for when I would like the article published.
  • In my script, if "Now" is selected, the data is written directly to the .txt file as shown above. If "Later" is selected, the data is written (with publishing timestamp) to a MySQL db.
  • I scheduled a cron job to run another php script every ten minutes to write any data with a timestamp that has passed to my .txt file and remove the entry from the db.

Works great!

奶气 2024-10-13 03:32:59

我想您可以在这里阅读更多相关内容,但有一个快速总结:

使用cron定期执行作业
安排(例如,运行自动磁带
每个工作晚上备份或生成
月末报告)。使用 at 运行
将来某个时候的一次工作。
使用它们来自动化您的
重复性任务!

我想你应该使用 at 来安排你的任务


但你也可以看看 Google 应用引擎 任务队列/cron< /a> 免费安排您的任务(大量配额)。它使用 webhooks 来自动执行任务和扩展。

You could read more about this over here I guess, but a quick summary:

Use cron to execute jobs on a regular
schedule (e.g., run an automatic tape
backup each work night or generate
end-of-month reports). Use at to run
a job once at some time in the future.
Use them both to automate your
repetitive tasks!

I guess you should use at to schedule your tasks


But you could also have a look at Google app engine task queue/cron to schedule your tasks for free(generous quota). It uses webhooks to execute tasks and scales automatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文