使用curl来定时执行一个url
我在 MediaTemple 上托管的 php-Codeigniter 上有一个应用程序,我想要一个运行控制器的 cron 作业,支持人员告诉我使用 CURL,我尝试使用但没有成功,例如:
curl http://mydomain.com/admin/action/get
我的控制器将一些信息插入数据库,curl不显示错误,但仅当我直接在浏览器上运行控制器时才不会发生插入。无需授权。
有什么想法吗?
谢谢
I have an application on php-Codeigniter hosted on MediaTemple and I want to have a cron job runing a controller, the support told me to use CURL I tried using without any succes, example:
curl http://mydomain.com/admin/action/get
My controller inserts some information to a database, the curl doesn't display error but the inserts are not happening only when I run the controller directly on the browser works. no auth is required.
any idea?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要提供有关正在发生的情况的更多信息,然后人们才能真正为您提供帮助。请参阅提问问题实用指南。
一些起点:
curl
如何提供它。如果没有,请注意您允许 Internet 上的任何人启动此过程,这可能会促进拒绝服务攻击或其他安全问题。You need to provide more information about what's happening before people can really help you. Please see this helpful guide to asking questions.
Some starting points:
curl
how to supply it. If not, be aware that you're allowing this process to be actuated by anyone on the Internet, which may facilitate denial-of-service attacks or other security issues.只需创建一个 cronactivator.php 文件,并将您的 cronjob 指向该文件。不需要 CURL。
Just make a cronactivator.php file, and point your cronjob to this file. No CURL needed.
@seth - 我不在 Linux 机器上,所以无法真正测试这一点,但你的问题是 curl 本身未安装 或 curl 需要某些特定的开关才能工作强>。
如果您仍然希望它工作,首先通过服务器的 SSH(在 Windows 上使用 Putty)让它工作,或者您可以编写一个脚本来测试它;
并不是说如果它写入 0 以外的任何其他错误代码,那么 CURL 就会出现问题。
另一方面,您可以使用
wget
来代替。它在 Linux 上比 CURL 更受支持(据我所知)。虽然我是 CURL 的忠实粉丝,但碰巧wget
在此类工作中更受欢迎。@seth - I'm not on a linux box so can't really test this out, but your issue is either curl itself is not installed or curl needs certain specific switches to work.
If you still want it to work, first get it to work via the server's SSH (use Putty on Windows) or alternatively you can write a script to test it out;
Not that if it writes any other error code than 0, you have issues with CURL.
On the other hand, you can use
wget
instead. It is more supported on Linux than CURL (afaik). Though I'm a big fan of CURL, it just so happens thatwget
is more popular for this kind of work.或者...您可以编写一个旨在在 CLI 上运行的 PHP 脚本,并将 cron 任务直接分配给 php 文件:
Heres 有关在 CLI 上使用 PHP 的一些信息。使用 CodeIgniter 有方法来执行此操作。
也就是说,有一种方法可以确定请求是否发送到任何地方。 (除了保存请求的输出之外)CodeIgniter 配备了出色的日志记录方法。您可以将 application/config/config.php 中的
$config['log_threshold']
设置为类似3
的值。在浏览器中运行该请求,然后在 CLI 中运行一次。然后在您的 application/logs 文件夹中查找当天的日志文件。在文件中查找您请求的 URL。应该有两个地方记录它。如果日志文件中的请求数少于您发出的请求数,那么您就知道 CURL 出现问题。否则,您就知道该应用程序出了问题。Or... you could just write a PHP script that is intended to run on the CLI and assign the cron task directly to the php file:
Heres some information on using PHP on the CLI. There are ways to do this with CodeIgniter.
That said, there's a way to find out for sure if the request is going anywhere. (besides saving the output of the request) CodeIgniter is equipped with excellent logging methods. You could set your
$config['log_threshold']
in application/config/config.php to something like3
. Run the request in both a browser, and then once in the CLI. Then look in your application/logs folder for log file for the current day. Find within the file the URL that you're requesting. There should be two places where it is logged. If there's less than the number of requests you made in the log file, then you know that something is wrong with CURL. Otherwise, you know something is wrong with the app.