Curl does not execute properly when php is called

So, I want to execute the bash command from PHP on my web server. I can do this using shell_exec . However, one of the commands I want to execute is curl . I use it to send the .wav file to another server and record its response. But when called from the PHP curl does not work.

I reduced the error to the following small example. I have a script called php_script.php that contains:

 <?php $ver=shell_exec("curl -F file=@uploads /2013-7-24-17-31-43-29097-flash.wav http://otherserver"); echo $ver 

Curiously, when I run this php script from the command line using php php_script.php , the result is

 Status: 500 Internal Server Error Content-type: text/html 

However, if I run curl -F file=@uploads /2013-7-24-17-31-43-29097-flash.wav http://otherserver directly, I get the answer that I expected:

 verdict = authentic 

(Edit :) I should probably mention that if I put some bash code inside a shell_exec argument that does not contain curl , the bash command will execute normally. For example, changing the string to $ver = shell_exec("echo hello > world"); puts the word "hello" in the file "world" (provided that it exists and is writable). (Editing the end).

Something is blocking curl execution when it is called from PHP. I thought it could be PHP running in safe mode, but I did not find any signs of this in php.ini. (Is there a way to check this to make sure 100%?) What blocks curl and, more importantly, how can I bypass or disable this block?

(And yes, I understand that PHP has a curl library. However, I prefer to use commands that I can run from the command line for debugging purposes.)

amuses, Alan

+4
source share
2 answers

The reason is the administrative privileges when running the command, when you run it as root, and thus the command is launched. But when you run the command through PHP, it runs as a user. By default, the user does not have privileges to run shell_exec commands.

You must change shell_exec settings through the CPanel / Apache configuration file. But it is not recommended to give the user access to shell_exec, since it helps hackers to attack the server and, therefore, caution should be exercised.

It would be more appropriate to use the curl library provided in PHP.

0
source

This is the code snippet that uses curl in php. This code is actually recorded on the website and posts, but at least you can see how curl is used:

 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $host); curl_setopt($ch, CURLOPT_VERBOSE, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_POST, 1); $result = curl_exec($ch); // Look at the returned header $resultArray = curl_getinfo($ch); curl_close($ch); 
-one
source

Source: https://habr.com/ru/post/1493250/


All Articles