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
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'm using PHP with a CRON job to get the contents of a URL using:
$content = file_get_contents("http://www.example.com");
I then use this content to send as an HTML newsletter. I'm running PHP 7, and I have enabled the relevant PHP INI settings:
allow_url_fopen = On
However, the $content variable is still turning up empty and my error_log is showing the following error:
PHP Warning: file_get_contents(http://www.example.com):
failed to open stream: no suitable wrapper could be found
in /home/xxxx/public_html/cron.php on line xxx
Everything else in the cron.php is working as expected.
Additionally if I manually visit the cron.php via a browser, file_get_contents is working perfectly.
Running WHM/cPanel etc.
Any help is greatly appreciated.
Get path to php.ini in you script:
php_ini_loaded_file();
set path to php.ini:
/opt/php/7.3/bin/php -c /var/www/u1234567/php-bin/php.ini -f /var/www/u1234567/public_html/site.com/script.php
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 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.
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.