Is it possible to convert a CURL command to PHP? - 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.

Related

PHP - Sending request causes test server to freeze [duplicate]

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.

Why is PHP Curl returning T_CONSTANT_ENCAPSED_STRING?

I'm attempting to use PHP to get data from CallRail.com API via JSON.
However, I'm getting this error when previewing my PHP file directly in my browser: T_CONSTANT_ENCAPSED_STRING
My code:
<?
curl -H "Authorization: Token token={my-token-id}" \
-X GET \
"https://api.callrail.com/v2/a/{my-account-id}/calls.json"
?>
So far I have:
Read several articles related to the error without finding a solution or verifiably accurate explanation.
I've manually retyped all of the code to ensure I didn't have any improperly encoded characters or invalid white spaces.
I've also attempted to locate official documentation in the PHP docs but not found anything helpful.
And I've tested this in a blank PHP file so the code is completely isolated.
I don't know how to further debug this issue and am hoping someone can either identify the problem or share the steps to debug this on my own from this point.
Your code sample is binary, to be executed from the command line in a shell like Bash, not PHP. This is why you're getting that error.
If you want PHP to call an external binary use exec(), shell_exec() or system().
Also, for portability, don't use the short open tags, ever, because it depends on the short_open_tag php.ini directive, or whether or not PHP was compiled with --enable-short-tags.
Even if your code doesn't have to be portable, it's just a bad habit. In case you ever need to work with some portable PHP code in the future; try:
<?php
echo shell_exec('curl -H "Authorization: Token token={my-token-id}" -X GET "https://api.callrail.com/v2/a/{my-account-id}/calls.json"');
Or if you want it on multiple lines, you could use implode:
<?php
echo shell_exec ( implode ( " ", [
'curl',
'-H "Authorization: Token token={my-token-id}"',
'-X GET',
'"https://api.callrail.com/v2/a/{my-account-id}/calls.json"'
] ) );

how to know curl file called via cli or apache2handler

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.

PHP Built-In Server Can't cURL

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.

Run PHP file which contains curl [duplicate]

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

Categories