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.

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料