从另一个PHP脚本执行一个PHP脚本 [英] Execute a PHP script from another PHP script

查看:367
本文介绍了从另一个PHP脚本执行一个PHP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何通过使用php手动触发它来使服务器运行php脚本?基本上,我有一个很大的cronjob文件,每2小时运行一次,但是我希望能够自己手动触发文件,而不必等待文件加载(我希望在服务器端完成).

我想从php文件执行文件...不是命令行.

解决方案

您可以从命令行手动调用PHP脚本

hello.php
<?php
 echo 'hello world!';
?>

Command line:
php hello.php

Output:
hello world!

请参阅文档: http://php.net/manual/zh/features. commandline.php


EDIT (OP)编辑了问题以添加关键细节:该脚本将由另一个脚本执行.

有两种方法.首先,也是最简单的,您可以仅包含文件.当您包含文件时,其中的代码将执行"(实际上是解释的).任何不在函数或类主体中的代码都将被立即处理.看看include的文档( docs )和/或require(文档)(请注意:include_oncerequire_once是相关的,但是在重要的方面有所不同.请查看文档以了解不同之处)您的代码如下所示:

 include('hello.php');
 /* output
 hello world!
 */

第二个,稍微复杂一点的是使用shell_exec( docs ).使用shell_exec,您将调用php二进制文件并将所需的脚本作为参数传递.您的代码如下所示:

$output = shell_exec('php hello.php');
echo "<pre>$output</pre>";
/* output
hello world!
*/

最后,也是最复杂的,您可以使用CURL库来调用文件,就像通过浏览器请求该文件一样.在此处查看CURL库文档: http://us2.php.net/manual /en/ref.curl.php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.myDomain.com/hello.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true)

$output = curl_exec($ch);
curl_close($ch);
echo "<pre>$output</pre>";
/* output
hello world!
*/

所用功能的文档

How would I make my server run a php script by triggering it manually using php? Basically I have a pretty big cronjob file that is ran every 2 hours, but I want to be able to trigger the file manually myself without having to wait for it to load (i want it to be done on the server's side).

EDIT: I want to execute the file from a php file... Not command line.

解决方案

You can invoke a PHP script manually from the command line

hello.php
<?php
 echo 'hello world!';
?>

Command line:
php hello.php

Output:
hello world!

See the documentation: http://php.net/manual/en/features.commandline.php


EDIT OP edited the question to add a critical detail: the script is to be executed by another script.

There are a couple of approaches. First and easiest, you could simply include the file. When you include a file, the code within is "executed" (actually, interpreted). Any code that is not within a function or class body will be processed immediately. Take a look at the documentation for include (docs) and/or require (docs) (note: include_once and require_once are related, but different in an important way. Check out the documents to understand the difference) Your code would look like this:

 include('hello.php');
 /* output
 hello world!
 */

Second and slightly more complex is to use shell_exec (docs). With shell_exec, you will call the php binary and pass the desired script as the argument. Your code would look like this:

$output = shell_exec('php hello.php');
echo "<pre>$output</pre>";
/* output
hello world!
*/

Finally, and most complex, you could use the CURL library to call the file as though it were requested via a browser. Check out the CURL library documentation here: http://us2.php.net/manual/en/ref.curl.php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.myDomain.com/hello.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true)

$output = curl_exec($ch);
curl_close($ch);
echo "<pre>$output</pre>";
/* output
hello world!
*/

Documentation for functions used

这篇关于从另一个PHP脚本执行一个PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆