[英]Run a cron job from within a php file
I have a problem where my hosting company won't let me run a cron job in this format from my control panel: 我遇到一个问题,我的托管公司不允许我从控制台通过这种格式运行cron作业:
/usr/bin/php /home/sites/MYDOMAIN.com/index.php?option=com_community&task=cron
Or: 要么:
www.MYDOMAINNAME.com/index.php?option=com_community&task=cron
Now if i run the second job in a browser ie: 现在,如果我在浏览器中运行第二个作业,即:
www.MYDOMAINNAME.com/index.php?option=com_community&task=cron
this works fine in a browser 这在浏览器中工作正常
My support says I have to create a file to run the URL. 我的支持者说我必须创建一个文件来运行URL。 The only problem is I don't know how to run a URL in PHP. 唯一的问题是我不知道如何在PHP中运行URL。 I have asked a few sites. 我问了几个站点。 But nothing. 但是什么都没有。 My file is called bump.php
and has the following code: 我的文件名为bump.php
,具有以下代码:
lynx -dump http://www.MYDOMAIN.com/index.php?option=com_community&task=cron
this is what i have in the file 这是我在文件中所拥有的
<?php
echo file_get_contents('DOMAIN.com/index.php?option=com_community&task=cron');
?>
You have to access the file in question via your webserver, not directly by file access. 您必须通过网络服务器访问有问题的文件,而不是直接通过文件访问。 If you access it by file-access, it will just return the php code and not execute it. 如果通过文件访问来访问它,它将仅返回php代码而不执行它。
There are several options on how to access files via webserver. 关于如何通过网络服务器访问文件,有几种选择。 One is your shown method with file_get_contents
. 一种是显示的file_get_contents
方法。 You will need to add http://
in front of the url to tell PHP that you want it accessed remotly and not as a local file. 您将需要在URL前面添加http://
,以告诉PHP您希望其远程访问而不是作为本地文件访问。
file_get_contents
is not allways configured to allow remotely downloading files. 并非始终将file_get_contents
配置为允许远程下载文件。 In these cases, it will not work. 在这些情况下,它将不起作用。 You can check this link to see the configuration setting for remote accessing files: http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen 您可以检查此链接以查看用于远程访问文件的配置设置: http : //www.php.net/manual/zh/filesystem.configuration.php#ini.allow-url-fopen
Another solution is to use the curl extension (if available) 另一个解决方案是使用curl扩展(如果有)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
http://www.php.net/manual/en/function.curl-exec.php http://www.php.net/manual/zh/function.curl-exec.php
There are other extensions if curl is also not available... 如果curl也不可用,还有其他扩展名...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.