dq13733519262 2018-04-08 16:03 采纳率: 0%
浏览 71
已采纳

是否可以将CURL命令转换为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\LocalStateootfs\home\User\Pastec_FYP\CurlCommand.sh

PHP

exec('CallCurlCommand.bat');

I do still wish there was a straight conversion to PHP but this works.

  • 写回答

1条回答 默认 最新

  • dreinuqm992401 2018-04-10 18:59
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?