I have call-cli.php
I am executing it via command line.
code of call-cli.php
echo "File 1 ".php_sapi_name(); // returns cli
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/curltest/step1.php?productId=12");
$response = curl_exec($ch);
curl_close($ch);
code of step1.php
echo " step1 ".php_sapi_name(); // returns apache2handler
if(php_sapi_name()==='cli') {
// To do execute code regarding cli
}
if(php_sapi_name()==='apache2handler') {
// To do execute code regarding apache2handler
}
When ADMIN run step1.php via browser it should execute apache2handler code and for cli the diff one.
I am getting productId from call-cli.php. So I need to invoke curl from call-cli.php
So I want to know is there any way to find curl called via cli file returns cli instead of apache2handler or any other suggestion?
No, as Curl opens the step1.php file like a browser via HTTP which is handled by the web server. Apache in this case, therefore it's apache2handler.
But PHP scripts will only output cli if executed from a terminal.
The curl call in call-cli.php is calling curl via cli, but since you are echoing the result of step1.php, which is served via Apache, you are seeing apache2handler in your code.
If you call step1.php via command line (e.g. php step1.php), you will see that it also returns cli.
Not sure what you are trying to achieve, so please clarify your questions.
Related
I have a relatively simple script like the following:
<?php
$url = "localhost:2222/test.html";
echo "*** URL ***\n";
echo $url . "\n";
echo "***********\n";
echo "** whoami *\n";
echo exec('whoami');
echo "* Output **\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
When I execute it on the command line, it works - I get the meager results from within test.html.
When I run this script by loading up the built-in PHP server and browsing to the script, it hangs. No output to the screen, nothing written to the logs.
I read that sometimes user permissions can get in the way, so I tried doing whoami to ensure that the user that ran the built-in PHP server is the same as the one who executed the script on the command line; which they are.
safe_mode is off, disable_functions is set to nothing. I can exec other commands successfully (like the whoami).
What else should I check for? Does the built-in PHP server count as someone other user when it fulfills a request perhaps?
The PHP built-in development web server is a very simple single threaded test server. It cannot handle two requests at once. You're trying to retrieve a file from itself in a separate request, so you're running into a deadlock. The first request is waiting for the second to complete, but the second request cannot be handled while the first is still running.
Since PHP 7.4 the environment variable PHP_CLI_SERVER_WORKERS allows concurrent requests by spawning multiple PHP workers on the same port on the built-in web server. It is considered experimental, see the docs.
Using it, the PHP script can send requests to itself which is already being served, without halting.
PHP_CLI_SERVER_WORKERS=10 php -S ...
Works with Laravel as well:
PHP_CLI_SERVER_WORKERS=10 php artisan serve
I think problem in your $url. It may be look like this $url = "http://localhost:2222/test.html"; or $url = "http://localhost/test.html"; I think it's solve your problem. Thanks for your question. Best of luck.
I have a web application that I need to send a CURL command from a HTTP URL to an application which is running on Ubuntu.
The curl command is this:
curl -X POST --data-binary #/home/User/Pastec_FYP/Currency_Test_Images/Test_TenEuro.jpg http://127.0.0.1:4212/index/searcher
The command is getting an image from the following:
#/home/User/Pastec_FYP/Currency_Test_Images/Test_TenEuro.jpg
And it is searching through the index at
http://127.0.0.1:4212/index/searcher
I need to be able to translate that to PHP.
EDIT
This is what I got so far, but it's still saying image_not_decoded
$ch = curl_init();
$post = array(
"file" => "#" .realpath("/home/User/Pastec_FYP/Currency_Test_Images/Test_TenEuro.jpg")
);
curl_setopt_array(
$ch, array(
CURLOPT_URL => 'http://127.0.0.1:4212/index/searcher',
curl_setopt($ch, CURLOPT_POSTFIELDS, $post),
CURLOPT_RETURNTRANSFER => true
));
$output = curl_exec($ch);
echo $output;
curl_close($ch);
From past use of the physical Curl command in Ubuntu it used to return that error when the path to the Image wasn't right, but i know its right as it works in Command line.
So is there anything I should change?
Additional Edit (To get it working)
I got it working how I wanted, but probably a lot more long winded than needed, but it works. I wrote a CurlCommand.sh with the Curl command I wanted to execute, then called the .sh file from a batch script (CallCurlCommand.bat) opening Ubuntu and inserting the CurlCommand.sh into it. Then using PHP to call the batch file (CallCurlCommand.bat).
CurlCommand.sh
curl -X POST --data-binary '#/home/User/Pastec_FYP/Currency_Test_Images/Test_FiveEuro.jpg' 'http://localhost:4212/index/searcher'
CallCurlCommand.bat
C:\Users\User\AppData\Local\Microsoft\WindowsApps\ubuntu.exe< C:\Users\User\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\rootfs\home\User\Pastec_FYP\CurlCommand.sh
PHP
exec('CallCurlCommand.bat');
I do still wish there was a straight conversion to PHP but this works.
It seems you have bit of a special system - you seem to be running your server on windows, which has ubuntu as a subsystem and curl as well as your file which you post is in there.
If you want to run it directly from your PHP server, you could install curl on your Windows. One way of doing it is downloading Win32 binary of curl from https://curl.haxx.se/download.html. After that you should be able to do something like
$curlpath = 'C:\path\to\curl.exe';
$filepath = '/home/User/FYP_Pastec/Currency_Test/Test_FiveEuro01.jpg';
$url = 'http://localhost:4212/index/searcher';
exec("$curlpath -X POST --data-binary \"#$filepath\" \"$url\"");
which would then send it.
I have a relatively simple script like the following:
<?php
$url = "localhost:2222/test.html";
echo "*** URL ***\n";
echo $url . "\n";
echo "***********\n";
echo "** whoami *\n";
echo exec('whoami');
echo "* Output **\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
When I execute it on the command line, it works - I get the meager results from within test.html.
When I run this script by loading up the built-in PHP server and browsing to the script, it hangs. No output to the screen, nothing written to the logs.
I read that sometimes user permissions can get in the way, so I tried doing whoami to ensure that the user that ran the built-in PHP server is the same as the one who executed the script on the command line; which they are.
safe_mode is off, disable_functions is set to nothing. I can exec other commands successfully (like the whoami).
What else should I check for? Does the built-in PHP server count as someone other user when it fulfills a request perhaps?
The PHP built-in development web server is a very simple single threaded test server. It cannot handle two requests at once. You're trying to retrieve a file from itself in a separate request, so you're running into a deadlock. The first request is waiting for the second to complete, but the second request cannot be handled while the first is still running.
Since PHP 7.4 the environment variable PHP_CLI_SERVER_WORKERS allows concurrent requests by spawning multiple PHP workers on the same port on the built-in web server. It is considered experimental, see the docs.
Using it, the PHP script can send requests to itself which is already being served, without halting.
PHP_CLI_SERVER_WORKERS=10 php -S ...
Works with Laravel as well:
PHP_CLI_SERVER_WORKERS=10 php artisan serve
I think problem in your $url. It may be look like this $url = "http://localhost:2222/test.html"; or $url = "http://localhost/test.html"; I think it's solve your problem. Thanks for your question. Best of luck.
So I want to execute a 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 a .wav file to another server and record its response. But when invoked from PHP, curl doesn't work.
I reduced the error to the following small example. I have a script named php_script.php which contains:
<?php
$ver=shell_exec("curl -F file=#uploads/2013-7-24-17-31-43-29097-flash.wav http://otherserver");
echo $ver
The curious thing is that when I run this php script from command line using php php_script.php, the result I get 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 response I was expecting:
verdict = authentic
(Edit:) I should probably mention that if I put some bash code inside the shell_exec argument which does not contain curl, the bash command executes fine. For example, changing the line to $ver = shell_exec("echo hello > world"); puts the word "hello" into the file "world" (provided it exists and is writable). (End edit.)
Something is blocking the execution of curl when it is invoked from PHP. I thought this might be PHP's running in safe mode, but I found no indication of this in php.ini. (Is there a way to test this to make 100% sure?) What's blocking curl and, more importantly, how can I bypass or disable this block?
(And yes, I realize PHP has a curl library. However, I prefer to use commands I can run from the command line as well, for debugging purposes.)
cheers,
Alan
The reason is the administrative privileges when you run the command directly you are running it as root and thus the command gets executed. But, when you run the command through PHP it runs as an user. By, default user has not the privileges to run the shell_exec commands.
You have to change the settings of shell_exec through CPanel/Apache config file. But, it is not recommended to provide the shell_exec access to the user as it help hackers to attack on server and thus, proper care should be taken.
It would be more appropriate to use the curl library provided in PHP.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to enable php curl for use in php cli
I am running a file which containing curl function. When i run in command prompt i am getting below error.
test.php
<?php
echo "Hai";
$url="http://www.google.com/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$str = curl_exec($ch);
curl_close($ch);
echo $str;
?>
Command : C:\>test>php test.php
Fatal error: Call to undefined function curl_init()
When i run in web browser its running fine.
Information i got
<?php
dl("php_curl.dll");
...
?>
Not solved still
Probably you have different configuration for web servers and cli...
Try checking if there is more php.ini files in your filesystem
This will happen when the curl extension is not enabled for command line PHP.
Check whether it is enabled or not in the /etc/php5/cli/php.ini file
This is the specific php.ini file for command line PHP.
It seems that curl has not been installed as your PHP extension.
You need the cURL library in order to run cURL commands from the command prompt. Try using Cygwin, or download the cURL executable. You can find it here: http://curl.haxx.se/dlwiz/?type=bin