我想从php文件执行文件...不是命令行.解决方案您可以从命令行手动调用PHP脚本hello.php<?php echo 'hello world!';?>Command line:php hello.phpOutput:hello world!请参阅文档: http://php.net/manual/zh/features. commandline.php EDIT (OP)编辑了问题以添加关键细节:该脚本将由另一个脚本执行.有两种方法.首先,也是最简单的,您可以仅包含文件.当您包含文件时,其中的代码将执行"(实际上是解释的).任何不在函数或类主体中的代码都将被立即处理.看看include的文档( docs )和/或require(文档)(请注意:include_once和require_once是相关的,但是在重要的方面有所不同.请查看文档以了解不同之处)您的代码如下所示: include('hello.php'); /* output hello world! */第二个,稍微复杂一点的是使用shell_exec( docs ).使用shell_exec,您将调用php二进制文件并将所需的脚本作为参数传递.您的代码如下所示:$output = shell_exec('php hello.php');echo "<pre>$output</pre>";/* outputhello 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>";/* outputhello world!*/ 所用功能的文档命令行: http://php.net/manual/en/features.commandline .php include: http://us2.php.net/manual/zh/function.include.php require: http://us2.php.net/manual/zh/function.require.php shell_exec: http://us2.php.net/manual/zh-CN/function.shell-exec.php curl_init: http://us2.php.net/manual/zh-CN/function.curl-init.php curl_setopt: http://us2.php.net/manual/zh-CN/function.curl-setopt.php curl_exec: http://us2.php.net/manual/zh-CN/function.curl-exec.php curl_close: http://us2.php.net/manual/zh-CN/function.curl-close.phpHow 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 linehello.php<?php echo 'hello world!';?>Command line:php hello.phpOutput:hello world!See the documentation: http://php.net/manual/en/features.commandline.phpEDIT 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>";/* outputhello 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>";/* outputhello world!*/Documentation for functions usedCommand line: http://php.net/manual/en/features.commandline.phpinclude: http://us2.php.net/manual/en/function.include.phprequire: http://us2.php.net/manual/en/function.require.phpshell_exec: http://us2.php.net/manual/en/function.shell-exec.phpcurl_init: http://us2.php.net/manual/en/function.curl-init.phpcurl_setopt: http://us2.php.net/manual/en/function.curl-setopt.phpcurl_exec: http://us2.php.net/manual/en/function.curl-exec.phpcurl_close: http://us2.php.net/manual/en/function.curl-close.php 这篇关于从另一个PHP脚本执行一个PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-13 10:13