Run WP-CLI using PHP - php

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

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>";
?>

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

Related

PHP, system() and inheriting PATH to script

Php pages on /var/www/mypage say system("foo.sh"). foo.sh is located on /one/dir and contains command bar.sh that is in /other/dir. For a user using ssh connection and having /one/dir and /other/dir in PATH everything works.
Now, if I put
<Directory /var/www/mypage>
SetEnv PATH . . . :/one/dir:/other/dir
to Apache config, then print getenv("PATH"); on /var/www/mypage/mytest.php shows that /one/dir and /other/dir are in the path. But system() still does not found foo.sh. What's wrong? And after this also foo.sh should see the same PATH so that it will find bar.sh.
I am setting this on Ubuntu 20.04, PHP 7.4.3.
Most likely because just executing > foo.sh would show command not found: foo.sh.
You should specify full/relative path: system('./foo.sh');

PHP SNMP Warning: SNMP::get(): No response from 127.0.0.1

I'm trying to get PHP SNMP to work on XAMPP PHP 7.1.9
I'm always getting this result: Warning: SNMP::get(): No response from 127.0.0.1
Here's what I did:
Enabled php_snmp.dll on php.ini
Installed net-snmp-5.5.0-2.x64
Added windows environment variable MIBDIRS with value C:\usr\share\snmp\mibs
Code I'm running to test:
<?php
$session = new SNMP(SNMP::VERSION_1, "127.0.0.1", "public");
$sysdescr = $session->get("sysDescr.0");
echo "$sysdescr\n";
$session->close();
I'm new to this so I'm not sure what I'm missing.
The problem was with the MIBDIRS path.
I followed php docs and it says:
The Windows distribution of Net-SNMP contains support files for SNMP in the mibs directory. This directory should added to Windows' environment variables, as MIBDIRS, with the value being the full path to the mibs directory: e.g. c:\usr\mibs.
Problem was I couldn't find c:\usr\mibs on my net-snmp installation so I used C:\usr\share\snmp\mibs instead.
Solution:
I tried using a device with snmp support instead of 127.0.0.1 and ran snmpget on cli and confirmed net-snmp installation actually works. I figured the problem must be with the path php is using to run snmp.
So I used where snmpget to check the command path then changed MIBDIRS environment value with the result which is C:\usr\bin\snmpget.exe

yaml_emit_file() not working. Call to undefined function yaml_emit_file()

The php function yaml_emit_file() is not working. I have installed and included the php_yaml.dll in my php.ini file restarted the server but still when I use this function, I get this error (when I run composer):
Call to undefined function RS\composer\yaml_emit_file()
Okay so a little about the background:
PHP version 7.1.7 & Composer version 1.5.1
I am using this function in a ScriptHandler.php file which is invoked when Composer is run. In this script I have a function buildModuleList which is called on post-update-cmd event of Composer. Everything else in the code is working fine.
I am in doubt that maybe I am using this function in wrong context or something like that.
Here is the code snippet where I am using yaml_emit_file() (Providing this just for reference, tell me if am using it the wrong way!):
if (!$fs->exists($moduleListFile)) {
$fs->touch($root.'/profiles/thunder/modulelist.yml');
$fs->chmod($root . '/profiles/thunder/modulelist.yml', 0666);
if(!empty($moduleList)){
$createyml= yaml_emit_file($moduleListFile, $moduleList);
if (!$createyml){
$io->writeError('<error>Cannot create modulelist.yml</error>');
}
}
$io->write('Success: Created new modulelist.yml', $newline= TRUE);
}
else{
$fs->file_put_contents($moduleListFile, $installedPackage, FILE_APPEND);
$io->write('Success: Module entry in modulelist.yml', $newline= TRUE);
}
i hope this help someone i test it on
Windows 10
XAMPP v3.2.4
PHP 8.0.2
Download the latest YAML DLL Package from : https://pecl.php.net/package/yaml
Unzip the files
Move the php_yaml.dll file to xampp/php/ext folder
Open your php.ini in xampp/php add a new line extension=php_yaml.dll, save and exit
Restart your XAMPP Servers
make a PHP file put into
<?php
phpinfo();
?>
Search the string yaml on the page. If it says Enabled then your YAML Extension is working.

Doctrine Cli on Windows

I am having some trouble configuring doctrine orm on windows 8, php 5.4. I have installed Doctrine using Composer.
I have followed the docs to the letter but when I run any commands, php vendor/bin/doctrine orm:schema-tool:create for example, my command line just outputs
SRC_DIR="`pwd`"
cd "`dirname "$0"`"
cd "../doctrine/orm/bin"
BIN_TARGET="`pwd`/doctrine"
cd "$SRC_DIR"
"$BIN_TARGET" "$#"
I have also tried php vendor/bin/doctrine.php .... but it just prints out the above.
I have followed Doctrine's guide to the letter. Has anyone seen this before and if so, can you suggest anything?
i found a solution
there are also a bin folder in vendor/doctrine/orm/bin/ you can use this one like this
php vendor/doctrine/orm/bin/doctrine orm:schema-tool:create
make sure you have root folder and a cli-config.php file is present in root folder.
below is location where i found a solution
https://groups.google.com/forum/#!msg/doctrine-user/_ph183Kh-5o/_P_coljB-dcJ
this is working fine for me .
I had the same problem. The following solution worked for me:
"vendor/bin/doctrine.bat" orm:schema-tool:create
So, basically you:
use the ".bat" file provided by Doctrine and
enclose the call to that ".bat" file in quotes.
My Environment
Windows 7 Professional (x64)
PHP 5.5.12
Doctrine ORM 2.4.4
Do not write "php..." (it will write the file content)
Just "vendor\bin\doctrine orm:schema-tool:create" do the job (from the project root eg. c:\php\theProject).
Next, you will need a "cli-config.php" in the project root...
For Window version use backward slash "\"
vendor\bin\doctrine orm:schema-tool:create
not forward slash "/"
vendor/bin/doctrine orm:schema-tool:create
You can either install something like git bash or simply use the PHP version of the script:
php vendor\bin\doctrine.php orm:info
Obviously, the php binary directory should be in your PATH environment variable, otherwise, it's something like:
C:\path\to\php.exe vendor\bin\doctrine.php orm:info
Copy doctrine.bat (located at vendor/bin/doctrine.bat) to your project root directory
Create a bootstrap.php file in any path inside your project root directory with the following content:
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
$paths = array("../model");
$isDevMode = false;
$dbParams = array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'user' => 'root',
'password' => '',
'dbname' => 'angular_php',
);
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$entityManager = EntityManager::create($dbParams, $config);
Create a cli-config.php file in your project root directory with the following content:
<?php
use Doctrine\ORM\Tools\Console\ConsoleRunner;
// replace with file to your own project bootstrap
require_once 'path/to/file/bootstrap.php';
return ConsoleRunner::createHelperSet($entityManager);
Execute from a command line window (CMD):
c:\path\to\project\root\directory>doctrine --help
It's done!
I found it wasnt returning anything from the doctrine.php.bat. Turns out it was a PHP error in my cli-config.php file
Using Cygwin on Windows with doctrine installed via composer, was having the same problem
resolved by:
vendor/bin/doctrine.bat orm:convert-mapping
if you are still having issues you can run the cli script using php to get the console tools running:
for example
php cli-config.php orm:schema-tool:create
All the answers on this question are either outdated or outright incorrect. In my case, I observed, after installing the the library, the "\vendor\doctrine\orm\" folder was completely empty. The docs ask you to run vendor/bin/doctrine, which in turn attempts to call "vendor\doctrine\orm\bin\doctrine[.php]" (The php file extension is optional). After discovering this, I downloaded the library from the git repository and replaced the composer-installed version.
php vendor/doctrine/orm/bin/doctrine
then works fine.
Also, beware of the common misconception that the cli-config.php file must exist in the root of your project. It's fine to leave it in your config folder

$PATH environment variable for apache2 on mac

I am trying to get apache/php to recognize the path to my git. I have been researching and looking in all corners of the web and cannot find how to do this. Basically, no matter what I try, when I run echo phpinfo(); the Apache Environment path does not change from /usr/bin:/bin:/usr/sbin:/sbin. And when I run system('echo $PATH'); in PHP, it reads the same.
System Information:
Mac OSX (Lion)
Apache 2 (running as _www)
PHP 5.3.6
Here is what I have tried editing so far:
/etc/profile
~/.bash_profile
~/.profile
/etc/path
/etc/path.d/{NEW_FILE}
Nothing I have tried so far has changed the $PATH variable. Any ideas?
SOLUTION
So here is the final solution. I edited the
/System/Library/LaunchDaemons/org.apache.httpd.plist
and added
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/git/bin</string>
</dict>
You can set the PATH environment variable in /System/Library/LaunchDaemons/org.apache.httpd.plist.
More in the docs.
Did you update the PATH environment variable of user '_www'? Apache will read environment variables from the user runs itself. Or, it looks like you didn't restart apache after updating PATH environment variable.
Check out the older discussion :
How do I add paths to the Apache PATH variable?
Setting environment variables in OS X?
And if you want to modify environment variable in PHP, getenv() and putenv() can be a better choice.
getenv : http://php.net/manual/en/function.getenv.php
putenv : http://www.php.net/manual/en/function.putenv.php
$path = getenv('PATH');
putenv( "PATH=$path:/new_path_that_you_want_to_add" );
Important note for El Capitan (Apologies for the new answer - I don't have enough Rep to comment)
On OSX 10.11, the /System/Library folder is protected, so the files can't be edited.
You need to:
Reboot into Recovery Mode (hold CMD + r after the startup sound)
Once in recovery mode, go to Utilities > Terminal
Run:
csrutil disable
Reboot back into OSX - you should now be able to change the files
Once done, go back to recovery mode and run
csrutil enable
Hope that helps
I created this gist that helped me out from the information above:
https://gist.github.com/srayhunter/5208619
My problem was that PHP was not finding a program that we had installed under /usr/local/bin. Once we did the above it all worked and played nice on mac osx.
for ubuntu server, in /etc/apache2/envvars,
for centos server, in /etc/sysconfig/httpd,
to add:
export PATH=<your php cli path>
and restart apache
A similar problem to what I was having installing Derby. The way I solved it was by opening TextEdit. Select File > Open at this point press Shift + Command + . , this will allow you to view all the documents. Head to the user directory and search for a file called ".profile" . Open it and add the export VARIABLE= Value line for example:
export DERBY_HOME=/opt/local/share/java/derby/
Save the document and restart your terminal to see if the changes went into affect.

Categories