简体   繁体   中英

Execute a php script within a running php script

I'm trying to execute a php script from a php file, but it seems that i can't get the correct bin path of the php.exe. Its not doing anything as far as i can tell. Lets assume service-oauth.php is a simple echo.

EDIT3: I fixed it using php-cli instead of php or the php bin path, i must admit that i tried this before but it seems something else was off when i tried this (one of the first things i tried). The answer provided by Keith in Can't execute PHP script using PHP exec , so it ended being a duplicate :S, thanks for the help to those who commented.

EDIT2: I tried calling the script directly from the server console as @Dagon suggested and it works, both using the php env variable and the php path to the bin, its clear that the path is correct, but something is preveting to get the output or to run the script using the php exec() function

service-oauth.php

<?php echo "Hello there"; ?>

And this is my script:

   $basePath =  plugin_dir_path( __FILE__ ); # Wordpress function, asume it works.
   $fileToExc = $basePath . 'service-oauth.php';
   # PHP_BINDIR: /usr/local/bin
   # PHP_BINARY: /usr/bin/php
   # exec("which php") /usr/bin/php
   $phpPath = exec("which php");
   $output = exec("$phpPath $fileToExc");
   print_r($output);

There are a lot of answers on stackoverflow that recommend any of those 3 options i commenented in the code, but none of them seems to work, not sure if it is the path or something else that is not working. I've tested this script on my localmachine (windows) and it works (even though i had to use a hardcoded path to the bin since i wans't getting the correct path), but i'm testing on my production server (linux) and is not working.

Note: Let me be clear, that this is not a duplicate , i've tried the following answers in these posts and many others, and it didn't work for me:

How to get path of the php binary on server where it is located

Can't execute PHP script using PHP exec

PHP exec to run a file

How to call shell script from another shell script?

Execute a PHP script from another PHP script

I've also tried using .exe at the end of the binary(windows localmachine), using php-cli instead of php, and i've tested the excec function and it works, but not in this case. Also tried with a shebang in the called script. It's propably something simple that i'm not aware of but i've been spending a lot of hours searching and testing and nothing so far. Any help is appreciated.

EDIT:

Using scandir on the bin folder shows scandir("/usr/bin/")

Array(
 ...
 [632] => php
 [633] => php-cgi
 [634] => php-cli
 ...
)

Tested if in safe mode using ini_get('safe_mode'), but it seems off.

I fixed it using php-cli instead of php or the php bin path, i must admit that i tried this before but it seems something else was off when i tried this (one of the first things i tried). The answer provided by Keith in Can't execute PHP script using PHP exec , so it ended being a duplicate :S, thanks for the help to those who commented. – Zagen

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM