0

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.

Jay1995
  • 137
  • 1
  • 14
  • 1
    well, you could just `exec` the same command from php. Or you could use [curl library](http://php.net/manual/en/book.curl.php). Or you could use just php for the task. Or something else. Which do you want? – eis Apr 08 '18 at 21:19
  • 1
    So if you google `php curl`, the first and third results are links to the PHP manual page for it's built-in Curl library. The second result is a link to a related StackOverflow question, and the fourth result is a beginners tutorial on how to write Curl in PHP. I'm guessing you didn't do much research before asking the question, right? – Spudley Apr 08 '18 at 21:28
  • @eis what do you mean execute the same command from php? – Jay1995 Apr 08 '18 at 21:30
  • @Spudley I have no experience in converting curl to php or what exactly to be looking for but thank you I will have a look at them! I was hoping someone could help me convert it and explain how its done. – Jay1995 Apr 08 '18 at 21:32
  • @Jay1995, no offense, but you still won't have `any` experience converting cURL to PHP, if you just ask strangers on the internet to do all the work for you. Asking specific questions when you can't figure something out for yourself is different. – fubar Apr 08 '18 at 22:17
  • @fubar No offence taken, I didn't mean to come across like I was trying to get all the work done for me. Converting this cURL to PHP is one of many steps I have yet to do and was just wondering if someone did convert it and explained how it was converted it would help greatly as from reading the documents I am still unsure of what to do. – Jay1995 Apr 08 '18 at 22:26
  • by executing the same command from php, I mean `exec('/full/path/to/curl -X POST --data-binary @/home/User/Pastec_FYP/Currency_Test_Images/Test_TenEuro.jpg http://127.0.0.1:4212/index/searcher');` – eis Apr 08 '18 at 22:34
  • @eis ah okay and by the `full/path/to/curl` you mean where the cURL package is installed? Or do you mean store it in a .bat file and call the execution that way? – Jay1995 Apr 08 '18 at 22:46
  • I mean the location of your curl binary executable. The output of `which curl`. – eis Apr 08 '18 at 23:13
  • `exec('/mnt/c/Users/User/AppData/Local/Packages/CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc/LocalState/rootfs/usr/bin/curl -X POST --data-binary @/home/User/Pastec_FYP/Currency_Test_Images/Test_TenEuro.jpg http://127.0.0.1:4212/index/searcher');` if this is what you mean, it does not work – Jay1995 Apr 08 '18 at 23:32
  • 1
    then you need to do some debugging. I had a few pointers [here](https://stackoverflow.com/a/13903250/365237) that apply; mainly doing `2>&1` and printing out the output to find out any error messages. But yes, that's what I meant. – eis Apr 09 '18 at 05:25
  • @eis From what I think I've figured out its giving me the error of `illegal characters in the URL` ( `http://127.0.0.1:4212/index/searcher`)and from what I've gathered its due to the forward slash (`/`)... I've tried using it as `http:\\127.0.0.1:4212\index\searcher` and it does not work – Jay1995 Apr 09 '18 at 13:22
  • that makes no sense. Forward slash is not an illegal url. You could test putting the url into quotes, but I don't see why it would complain about it. – eis Apr 09 '18 at 13:24
  • I've tried putting it in quotes and even double quotes but it still complains – Jay1995 Apr 09 '18 at 13:36
  • @eis I apologise it is not the forward slash that was the problem it was the colon (`:`), does anyone know how to get around this? – Jay1995 Apr 09 '18 at 14:36
  • add your code to the question if you want help with it. – eis Apr 09 '18 at 14:41
  • Thank you @eis for all your help in putting me on the right track. – Jay1995 Apr 10 '18 at 14:19

1 Answers1

1

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.

eis
  • 51,991
  • 13
  • 150
  • 199
  • This doesn't seem to work though as the program that is running on Ubuntu is only able to run on Ubuntu 16.04 or higher. When I done the above script it didn't make contact with it. – Jay1995 Apr 11 '18 at 11:23
  • If I change it to this I get contact: `$curlpath = 'C:\DevPrograms\curl-7.59.0-win32-mingw\bin\curl.exe'; $filepath = '@/home/User/FYP_Pastec/Currency_Test/Test_FiveEuro01.jpg'; $url = 'http://localhost:4212/index/searcher'; $execution = exec("$curlpath -X POST --data-binary \"@$filepath\" \"$url\""); echo $execution;` but my response is `{"type":"IMAGE_NOT_DECODED"}` and this message only appears when it isn't getting the path right – Jay1995 Apr 11 '18 at 11:37
  • My sincerest apologies, it was a blunder on my part to do with the path. I had inserted an `@` at the start of `$filepath` as you can see above and not realised you had one down in the `exec`... Here is the final working one I ended up with: `$curlpath = 'C:\DevPrograms\curl-7.59.0-win32-mingw\bin\curl.exe'; $filepath = '/home/User/FYP_Pastec/Currency_Test/Test_FiveEuro01.jpg'; $url = 'http://localhost:4212/index/searcher'; $execution = exec("$curlpath -X POST --data-binary \"@$filepath\" \"$url\""); echo $execution; ` – Jay1995 Apr 11 '18 at 20:03
  • ok, great you got it working! I updated the answer accordingly, so feel free to accept the answer if the matter is resolved. – eis Apr 11 '18 at 20:08
  • don't suppose ya know how to execute a cURL command in Java also do you? – Jay1995 Apr 11 '18 at 20:16
  • @Jay1995 in java, one option is to use `Runtime.getRuntime().exec()` where in php you're using `exec()`. That might be easiest to implement. There is also newer ProcessBuilder interface, see [this](https://stackoverflow.com/questions/6856028/difference-between-processbuilder-and-runtime-exec) for details. – eis Apr 12 '18 at 05:10
  • which in your opinion would work best for executing the command above? I feel like the `Runtime.getRuntime().exec()` would be best? – Jay1995 Apr 12 '18 at 16:09
  • I don't see that much of a difference in most cases whichever you choose, but depends on your use case. – eis Apr 12 '18 at 19:26