What to do if php fails to call python

What if php fails to call python?

Solutions to PHP's failure in calling Python

Suppose there is a file: php_test.php python_test.py

To run Python in a php file:

exec("python python_test.py", $array, $ret);

If there is an error in running Python, it cannot be saved in array array, so you should redirect the standard error to a file. The above code is rewritten as follows:

exec("python python_test.py 2>error.txt", $array, $ret);

In bash, the three numbers of 0,1,2 represent stdin ﹐ fileno, stdout ﹐ fileno, stderr ﹐ fileno, that is, standard input (generally keyboard), standard output (generally display screen, precisely user terminal console), and standard error (error information output).

You can also redirect standard errors to standard output and save them to $array by:

exec("python python_test.py 2>error.txt 2>&1", $array, $ret);

Then we can find solutions according to the wrong information.

In general, you can see the error information immediately when you run the PHP file through the command on the terminal, but you can only output the error information by the above method when you run the PHP file through the browser. So the problem is that PHP can execute Python files successfully on the command line, but it can't run successfully through the browser, because the dynamic libraries called by the two methods are inconsistent!

The existing dynamic library in the system is called through the command line, while the dynamic library in the Web server is called through the browser. I installed XAMPP, so I called the dynamic library in lampp/lib through browser.

terms of settlement:

1. Find the error dynamic library in the output error information

2. Find the location of the relevant dynamic library through the locate command: locate libxxx.so

3. Delete or rename the dynamic library with the same name in the web server

4. Link the dynamic library found in the system in step 2 to the lib directory of the web server


There are two possible errors when using the hashlib module in Python

<span style="font-family:Arial, Helvetica, sans-serif;">relocation error: python: symbol OpenSSL_add_all_digests, version OPENSSL_1.0.0 not defined in file libcrypto.so.1.0.0 with link time reference</span>
<span style="font-family:Arial, Helvetica, sans-serif;">python: /opt/lampp/lib/libcrypto.so.1.0.0: version `OPENSSL_1.0.2' not found (required by /opt/lampp/lib/libssl.so.1.0.0)</span>

1. Find out all paths in the system including libcrypto.so.1.0.0: locate libcryto.so.1.1


2. Rename the dynamic library with the same name in the web server:

sudo mv /opt/lampp/lib/libcryto.so.1.1 /opt/lampp/lib/libcryto.so.1.1.bak

3. Link libcryto.so.1.1 in the system to the lib directory of the web server:

sudo ln -s  /lib/x86_64-linux-gnu/libcrypto.so.1.0.0  /opt/lampp/lib/libcryto.so.1.1

Tags: Programming Python PHP Web Server Ubuntu

Posted on Fri, 10 Jan 2020 01:22:32 -0500 by jonnyw6969