在php中触发脚本

在php中触发脚本,php,file,include,Php,File,Include,首先,我有一个php文件的cron作业。 其次,在cronjob调用的php文件中,我想调用一个带有参数的文件。在不更改php.ini的情况下,如何做到这一点 例如: include('file.php?id='.$id.'); 最简单的方法是使用文件获取内容: 对于更复杂的情况,您可以查看PHP Curl。您可以在其他文件中定义一个函数,并在原始文件中调用该函数。例如: file.php: function someFunction($x) { return $x*$x; } cron.

首先,我有一个php文件的cron作业。
其次,在cronjob调用的php文件中,我想调用一个带有参数的文件。在不更改php.ini的情况下,如何做到这一点

例如:

include('file.php?id='.$id.');

最简单的方法是使用文件获取内容:


对于更复杂的情况,您可以查看PHP Curl。

您可以在其他文件中定义一个函数,并在原始文件中调用该函数。例如:

file.php:

function someFunction($x)
{
 return $x*$x;
}
cron.php:

require 'file.php';
echo someFunction(10);

此外,在全局范围中定义的任何变量也将在其他文件中可用。例如,如果在cron.php中定义$foo,则可以在file.php中使用该值。虽然不建议这样做,但维护很多全局变量确实很困难

包含with get参数将不起作用,因为php的CLI版本不支持它。还包括基于文件系统的工作,而不是基于url的工作。include将/应该搜索文件file.php?id=1。您应该创建一个函数,默认情况下包含该文件,然后调用该函数


包含带有url的远程文件是一种非常糟糕的做法。如果你认为你必须这么做,你的想法很简单。尝试使用api/其他类型的接口与远程url交互。

根据include文档,如果其他php文件配置为parse.php而不是.txt,则是可能的。 请尝试以下代码:

/* This example assumes that www.example.com is configured to parse .php
* files and not .txt files. Also, 'Works' here means that the variables
* $foo and $bar are available within the included file. */

// Won't work; file.txt wasn't handled by www.example.com as PHP
include 'http://www.example.com/file.txt?foo=1&bar=2';

// Won't work; looks for a file named 'file.php?foo=1&bar=2' on the
// local filesystem.
include 'file.php?foo=1&bar=2';

// Works.
include 'http://www.example.com/file.php?foo=1&bar=2';

$foo = 1;
$bar = 2;
include 'file.txt';  // Works.
include 'file.php';  // Works.

用参数调用链接是什么意思?如果不更改php.ini,我怎么做?-如果更改php.ini,你怎么做-对此不确定,但请尝试执行,因为file\u get\u内容不会解析文件中的PHP。它将输出file.php的源代码。他想调用一个链接,但他没有说他需要输出或需要解析文件。我想触发该文件。就像按下一个按钮,它的动作就是文件。@Laurent:不,不会的。这将导致一个错误,即没有这样的文件不,它没有。与在浏览器中访问文件不同,它将输出文件内容。另外@zerkms有一个非常有效的观点,GET参数显然不能在文件系统上工作。这在简单的情况下是可行的,但有时将代码重构成一个简单的函数并不明显。例如,考虑像Kohana这样的框架-函数位于需要正确设置才能工作的控制器中。在这种情况下,使用Curl或file_get_内容直接调用URL是最简单的方法。我认为加载所有MVC内容比调用URL更好。如果您不需要Web服务器,为什么要使用它?为了防止一切出错,您可以修改$\u SERVER-Global以设置所有数据
/* This example assumes that www.example.com is configured to parse .php
* files and not .txt files. Also, 'Works' here means that the variables
* $foo and $bar are available within the included file. */

// Won't work; file.txt wasn't handled by www.example.com as PHP
include 'http://www.example.com/file.txt?foo=1&bar=2';

// Won't work; looks for a file named 'file.php?foo=1&bar=2' on the
// local filesystem.
include 'file.php?foo=1&bar=2';

// Works.
include 'http://www.example.com/file.php?foo=1&bar=2';

$foo = 1;
$bar = 2;
include 'file.txt';  // Works.
include 'file.php';  // Works.