PHP and CURL multipart/related - php

I'm trying to take a picture with a smartphone and send it to a OCR-Server.
I'm now having problem to post the picture with php to the Open-OCR server
Open-OCR provides a sample file howto upload local files to the server.
https://github.com/tleyden/open-ocr/blob/master/docs/upload-local-file.sh
The bash curl command that will be generated looks like this.
curl -v -X POST http://192.168.0.107:9292/ocr-file-upload --header 'Content-Type: multipart/related; boundary="2ac80ee2f500dbe0bae6d3148fe4f960"' --data-binary #-
How can I convert this command to PHP?
I'm really a noob in WEB-Dev and would really appreciate a sample php code that will imitate that command.

Related

How to upload file in dropbox using php or curl [duplicate]

I am searching everywhere and haven't been able to locate a suitable example and am not well versed enough to be able to sort it out via the docs. Could someone with more knowledge than I show me how to form the CURL command for OAUTH 2? And is it that I only need the OAUTH 2 secret key? I am being shown an App key, app secret and oauth 2. I am using this in a perl script if it matters.
The closest code I have found is this:
curl --request PUT --header "Content-Length: `ls -la jonathan.txt | awk '{ print $5}'`" --header
"Content-Type: multipart/mixed" --data-binary "#jonathan.txt" "https://api-
content.dropbox.com/1/files_put/dropbox/jonathan.txt?access_token=ABCDEF"
But I don't think that is OAUTH 2?
If you have an access token (created via the app console):
curl -H "Authorization: Bearer <your token>" https://api-content.dropbox.com/1/files_put/auto/ -T <your file path>
You need an access token for the account. (This is typically acquired by going through the OAuth flow, but you can also get one for your own account by clicking the "Generate" button on the page for your Dropbox app. See https://www.dropbox.com/developers/blog/94/generate-an-access-token-for-your-own-account.)
Once you have an access token, your curl command should probably work, though I prefer --header "Authorization:Bearer abc123xyz" over putting the access token in a query parameter. Also, drop the Content-Type: multipart/mixed, since that's not what you're sending.
I'd also recommend "auto" instead of "dropbox," just because it always does the right thing regardless of the app type.
Here is working codes to upload file in dropbox via CURL request.
curl -X POST https://content.dropboxapi.com/2/files/upload \
--header "Authorization: Bearer <your token>" \
--header "Dropbox-API-Arg: {\"path\": \"/file_path.txt\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}" \
--header "Content-Type: application/octet-stream" \
--data-binary "#file_path.txt"

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

How to interpret CURL headers sent by user

I am creating an API using Laravel and Apache as the backend. My predicament is this, when I type a curl command in my terminal like,
curl -H "API KEY: NIKAPIKEY" --data "param1=value1&param2=value2" http://localhost:8888/awesome-calendar/public/config
How do I read the header API KEY in my php backend? Like I can read the POST parameters as $_REQUEST.
Answered by #nogad
The function apache_request_headers acts like the var_dump for all HTTP headers
http://php.net/manual/en/function.apache-request-headers.php

Unable to POST file through curl using advanced rest client

Sending file to a php script works fine with curl from terminal but not through advanced rest client or postman.
Curl request-
curl -v --form filename=#/cus-check.json --form name=Upload http://127.0.0.1/cus.php
When sending the same request through ARC or Postman, the $_POST variable in php script returns an empty array.
Images

Preparing CURL request with php

I would like to have the following curl request generated using php CURL library for webdav.
curl -n -X MOVE "http://localhost/ocm/remote.php/webdav/dir1/subdir" -H "Destination: http://localhost/ocm/remote.php/webdav/dir1/"
Please help
Go through this..It shows all the curl option available to use in PHP along with examples...

Categories