简体   繁体   中英

Execute WP CLI commands in PHP script

To do this, I copied the repository WP CLI into Root WordPress

And I wrote the following PHP code to encode it and encountered the following errors

define('WP_CLI_ROOT', ABSPATH . 'wp-cli');
require_once WP_CLI_ROOT . '/php/wp-cli.php';
Warning: include_once(D:\Web\Sites\jupiterx/wp-cli/vendor/rmccue/requests/library/Requests.php): failed to open stream: No such file or directory in D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Bootstrap\IncludeFrameworkAutoloader.php on line 48

Warning: include_once(): Failed opening 'D:\Web\Sites\jupiterx/wp-cli/vendor/rmccue/requests/library/Requests.php' for inclusion (include_path='C:\xampp\php\PEAR') in D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Bootstrap\IncludeFrameworkAutoloader.php on line 48

Warning: include_once(D:\Web\Sites\jupiterx/wp-cli/vendor/wp-cli/mustangostang-spyc/Spyc.php): failed to open stream: No such file or directory in D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Bootstrap\IncludeFrameworkAutoloader.php on line 49

Warning: include_once(): Failed opening 'D:\Web\Sites\jupiterx/wp-cli/vendor/wp-cli/mustangostang-spyc/Spyc.php' for inclusion (include_path='C:\xampp\php\PEAR') in D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Bootstrap\IncludeFrameworkAutoloader.php on line 49

Warning: array_slice() expects parameter 1 to be array, null given in D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Runner.php on line 905

Warning: Invalid argument supplied for foreach() in D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Configurator.php on line 138

Warning: implode(): Invalid arguments passed in D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Runner.php on line 1024

Fatal error: Uncaught Error: Class 'cli\Colors' not found in D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Loggers\Regular.php:70 Stack trace: #0 D:\Web\Sites\jupiterx\wp-cli\php\class-wp-cli.php(881): WP_CLI\Loggers\Regular->error_multi_line(Array) #1 D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Runner.php(248): WP_CLI::error_multi_line(Array) #2 D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Runner.php(1083): WP_CLI\Runner::set_wp_root('D:\\Web\\Sites\\ju...') #3 D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Bootstrap\LaunchRunner.php(23): WP_CLI\Runner->start() #4 D:\Web\Sites\jupiterx\wp-cli\php\bootstrap.php(74): WP_CLI\Bootstrap\LaunchRunner->process(Object(WP_CLI\Bootstrap\BootstrapState)) #5 D:\Web\Sites\jupiterx\wp-cli\php\wp-cli.php(27): WP_CLI\bootstrap() #6 D:\Web\Sites\jupiterx\wp-content\plugins\tabanShahrSiteSaz\Plugin.php(72): require_once('D:\\Web\\Sites\\ju...') #7 D:\Web\Sites\jupiterx\wp-content\plugins\tabanShahrSiteSaz\app\core\SingletonPattern.php(26): TabanShahrSiteSaz\Plugin->includes() #8 D:\Web\Sites\jupiterx\wp-conten in D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Loggers\Regular.php on line 70

Thanks in advance for your help

WP-CLI can only be run correctly under the PHP cli SAPI, not under a web server API like cgi , apache or similar. This has some technical reasons, but it is also directly related to your server's security.

WP-CLI is a console application

The first technical reason is that WP-CLI is a console application, and it assumes access to the console input ( STDIN ) and output ( STDOUT ) streams. Within the web server context that WordPress runs under, these don't exist. The "input" for a webserver is the URL and payload of a web request, and the output is the HTML or JSON that is sent back to the browser.

WP-CLI is a wrapper around WordPress

The second technical reason is that WP-CLI is basically a "hack" that "wraps" an entire WordPress installation and executes it as a subroutine within its own process. If you would try to use WordPress to launch WP-CLI, you'd have a chicken & egg problem, or a software interpretation of "Inception".

WP-CLI is a sysadmin tool

The reason why this is also directly relevant in terms of security is that WP-CLI is, in the broadest sense, a sysadmin tool. It will directly create, modify, delete files, do direct requests against the database, and more. All of its operations can bypass any security measures that might have been put in place at the application level by WordPress and its plugins. Making WP-CLI accessible to the public-facing frontend and letting it be triggered via web requests would turn it into a critical security vulnerability. No server-side administration tool should be exposed to the frontend in whatever way.

Hopefully, this explains why what you want to do cannot be done and why you would not want to if you could.

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