3

I've installed WP-CLI on the Mac and my next step is to execute WP-CLI commands using PHP script.

I've tried to implement it the following way but I do not see anything happening. Can someone please look at my code and tell me that what I'm doing wrong?

define( 'WP_CLI_ROOT', '/usr/local/bin/wp' );
include WP_CLI_ROOT . '';
$output = shell_exec("wp --info");
echo "<pre>".$output."</pre>";

Do I need to configure and setup wp-cli with my PHP files?

Also, when I type wp --info on my terminal the following information comes up. Nothing is appearing beside the Package Dir & global config. do I also need to make adjustments to wp-cli?

MAC-00343:htdocs mike$ wp --info
PHP binary: /usr/bin/php
PHP version:    5.6.30
php.ini used:   
WP-CLI root dir:    phar://wp-cli.phar
WP-CLI vendor dir:  phar://wp-cli.phar/vendor
WP_CLI phar path:   /Users/mike/Docker/xamp/www/wordpress_wwws/htdocs
WP-CLI packages dir:    
WP-CLI global config:   
WP-CLI project config:  
WP-CLI version: 1.3.0

Any help or suggestions will be much appreciated.

Thanks

WKL
  • 37
  • 1
  • 4
  • Do you have a server like `MAMP` running? Are you working inside a wp directory? – RobBenz Aug 18 '17 at 14:35
  • I'm running WordPress on DOCKER. I'm not using WordPress docker image but have manually installed WP in a directory inside Docker. Docker/xamp/www/wordpress_wwws/htdocs But I'm not working inside the wp directory. I'm trying to execute wp-cli commands via custom PHP script. p.s: I've also tried my PHP code after installing WP-CLI Docker image. But it didn't work. – WKL Aug 18 '17 at 14:47
  • Prehaps you can try to shell-exec “php wp —info” ? – DGRFDSGN Nov 16 '17 at 07:20
  • See also [Running WPCLI in php script · Issue #1924 · wp-cli/wp-cli](https://github.com/wp-cli/wp-cli/issues/1924#issuecomment-268402112) – mems Sep 02 '20 at 23:09

2 Answers2

1

The problem is your wp cli aren't in the path environment variable, you should add your wp cli root to the path environment as following, hopefully this is helpful. For reference all wp cli command, visit the url - https://wpcommands.com

<?php
$saved = getenv("path");    // get the value of PATH environment variable and save current value
$newld = "/usr/local/bin";          // extra paths to add - in this case, it should be your wp cli root
if ($saved)
{
    $newld .= ":$saved";
}
// append old paths if any
putenv("path=$newld");        // set new value

// exec wp cli command
$output = shell_exec("wp --info");
echo "<pre>".$output."</pre>";


?>
Chung Nguyen
  • 481
  • 4
  • 6
0

enter image description here

Here is what wp --info returns on my terminal.

the only main difference I see is the php.isi used: field.

Sorry I cant help more, but I dont know what DOCKER is, and I am using MAMP, not XAMP

phar path is just the current directory I'm working in

RobBenz
  • 589
  • 1
  • 7
  • 21
  • Thanks for your help. But what about my PHP code? Is this the right way to execute wp-cli commands from a PHP script? e.g. I've also tried stuff suggested here but saw nothing in the output: https://stackoverflow.com/questions/37294485/using-wp-cli-through-php – WKL Aug 18 '17 at 14:54
  • kind of taking a guess here, but, if `wp --info` returns an array - the `echo` function would experience and `array -> string` conversion error, have you tried `var_dump` - please note this is speculation based on the accepted answer here https://wordpress.stackexchange.com/questions/219230/utilize-wp-cli-from-inside-wordpress-not-ssh -- `echo implode(PHP_EOL, $retval) . PHP_EOL;` – RobBenz Aug 18 '17 at 15:00
  • Yeah, I've tried var_dump and a number of other things. var_dump returns NULL. Thanks for your help :) – WKL Aug 18 '17 at 15:09