【问题标题】:Cron Job + TwitterCron 工作 + 推特
【发布时间】:2013-07-12 05:38:03
【问题描述】:

从 12:30 开始(一直到 1:30、2:30 等),我的应用程序会每隔一小时发布一条静态推文。我目前正在使用 themattharris 的 twitter API。我也有一个 cron 工作。

30 * * * *   php -q /home1/USER_NAME/public_html/twitter/index.php

我遇到的问题是当我手动刷新 index.php 和我的 cron 作业半工作时 PHP 代码工作。我从 cron 工作中收到的电子邮件是:

Error 200

我想知道的是,我怎样才能让我的 cron 工作?我的 cron 作业没有任何错误。

include 'tmhOAuth/tmhOAuth.php';

$tmhOAuth = new tmhOAuth(array(
    'consumer_key' => 'XXX',
    'consumer_secret' => 'XXX',
    'user_token' => 'XXX',
    'user_secret' => 'XXX',
));

// Bahrain's time zone
date_default_timezone_set ('Asia/Riyadh');

// Set status message
$timezone= date("g:i");
$tweetMessage = "It's now" .$timezone. "in Bahrain";

//Response
$response = $tmhOAuth->request('POST', $tmhOAuth->url('1.1/statuses/update'), array('status' => $tweetMessage));

//Do something if the request was unsuccessful    
if ($response != 200) {
    echo 'Error 200';
}

【问题讨论】:

标签: php unix cron twitter


【解决方案1】:

试试:

30 * * * * /usr/bin/php '/home1/USER_NAME/public_html/twitter/index.php'

这是正确的语法,但是在您的代码中看起来您实际上期望来自页面的响应,如果使用 cron,这将不起作用。

您可以像您在下面所说的那样使用 wget 获取实际 URL,或者另一种选择是使用 Curl 实际发布和请求该 URL。如果您使用 Curl,您将能够使用标准 cron,因为 Curl 会实际收到响应。

【讨论】:

  • 使用 JustHost,他们设置 cron 作业的方式类似于:_ _ _ _ _ |_| |_| |_| |_| |_|命令:--- 5 个框表示分钟、小时、每日、每月、每年(这是下拉菜单)命令行只是我的脚本所在的位置,在这种情况下是“www.domainname.com/twitter/index .php"
  • 是的,我想很多主机都是这样的,这让我很烦。我不喜欢这种方法,因为这会将您的 cron 作业放在一个可公开访问的页面上,任何碰巧请求该页面的人都在运行该脚本。如果是这种情况,有时我会将参数传递给脚本,例如 www.domainname.com/twitter/index.php?arg1=121。通过这种方式在脚本中,您可以验证某人是否只是点击了该页面 .../twitter/index.php 并运行 cron。我认为一般来说,让您的工作不是在您的网络服务器上提供的页面并将它们放在另一个文件夹中是一种更好的做法。
  • 嗯,我已经解决了这个问题,一切都很好,只是我的命令没有。我使用的命令是: wget -O /dev/null -q DOMAINNAME.com/twitter/index.php
猜你喜欢
  • 2013-07-11
  • 2012-09-13
  • 1970-01-01
  • 2012-08-09
  • 2017-05-23
  • 2017-07-07
  • 1970-01-01
  • 1970-01-01
  • 2011-03-27
相关资源
最近更新 更多