使用curl来定时执行一个url

发布于 2024-08-04 14:53:39 字数 261 浏览 5 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(4

巾帼英雄 2024-08-11 14:53:39

您需要提供有关正在发生的情况的更多信息,然后人们才能真正为您提供帮助。请参阅提问问题实用指南

一些起点:

  1. 当您从命令行运行该命令时会发生什么?
  2. 当您在 Web 浏览器中访问控制器 URL 时会发生什么?
  3. 你的“没有成功”到底是什么?您正在寻找哪些成功迹象以及您观察到了什么?
  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:

  1. What happens when you run that command from command line?
  2. What happens when you go to the controller URL in a Web browser?
  3. What exactly does your "not any success" consist of? What indications of success are you looking for and what are you observing?
  4. Does your controller require authentication? If so, you will need to tell 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.
梦在夏天 2024-08-11 14:53:39

只需创建一个 cronactivator.php 文件,并将您的 cronjob 指向该文件。不需要 CURL。

<?php 
//for php4 use fopen
$html_data = file_get_contents('http://www.yoursite.com/framework/controller/action', 'r');
echo "DONE!";
exit;

?>

Just make a cronactivator.php file, and point your cronjob to this file. No CURL needed.

<?php 
//for php4 use fopen
$html_data = file_get_contents('http://www.yoursite.com/framework/controller/action', 'r');
echo "DONE!";
exit;

?>
南冥有猫 2024-08-11 14:53:39

@seth - 我不在 Linux 机器上,所以无法真正测试这一点,但你的问题是 curl 本身未安装curl 需要某些特定的开关才能工作强>。

如果您仍然希望它工作,首先通过服务器的 SSH(在 Windows 上使用 Putty)让它工作,或者您可以编写一个脚本来测试它;

<?php
    $code=system('curl --help');
    echo '<br><br> Exit Code: '.$code;
?>

并不是说如果它写入 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;

<?php
    $code=system('curl --help');
    echo '<br><br> Exit Code: '.$code;
?>

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 that wget is more popular for this kind of work.

长途伴 2024-08-11 14:53:39

或者...您可以编写一个旨在在 CLI 上运行的 PHP 脚本,并将 cron 任务直接分配给 php 文件:

php -f /path/to/my/file.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:

php -f /path/to/my/file.php

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 like 3. 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.

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