I have been trying to implement a functionality that is written in Java, for uploading a file via PUT HTTP request.
Here is my sample Java code:
import com.google.common.io.Files;
public void uploadFile() {
final makeRestRequest makeUploadRequest = makeRequestTo(upload_file_location)
.method("PUT")
.addHeader(new HttpHeader("Accept", "application/json"))
.addHeader(new HttpHeader("Content-Type", "application/zip"))
.body(Files.newInputStreamSupplier(new java.io.File("/sample_file.zip")))
.build();
final getRestResponse uploadResponse = makeUploadRequest.fetchResponse();
}
I am looking for a similar variant of the php code, that can make a HTTP PUT call to upload a file, but i am not sure what to use for the newInputStreamSupplier. Here is my sample php code, that makes a PUT call to upload file using CURL, but fails to get a response:
<?php
$url = upload_file_location;
$localfile = "sample_file.zip";
$fp = fopen ($localfile, "rb");
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$headers = array();
$headers[] = 'Content-Type: application/zip';
$headers[] = 'Accept: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
$http_result = curl_exec($ch);
?>
I get an error saying: "File content is empty". Does anyone have an idea of what i am doing wrong?
Well, i figured out a solution for my question.
I should pass the full path to the file in the CURL filesize header:
curl_setopt($ch, CURLOPT_INFILESIZE, filesize(realpath($localfile));
Also, I had to add the below CURL header to transfer file data as binary:
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
The solution mentioned in this question helped me.
Related
I am trying to reproduce this POST https://www.screencast.com/t/OUE1StfLfyP7 in php but I couldn't archive that it works correctly because after downloading the image is not possible to open it
$url = "https://internal-api-staging-lb.interact.io/v2/contacts/X/documents/fr_FR9.gif";
$data = array('file' => '#'.realpath('https://www.viventura.de/images/icons/flags/fr_FR.gif'));
// Initialize cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/octet-stream", "authToken: XX"));
// Set URL on which you want to post the Form and/or data
curl_setopt($ch, CURLOPT_URL, $url);
// Data+Files to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// Pass TRUE or 1 if you want to wait for and catch the response against the request made
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// For Debug mode; shows up any error encountered during the operation
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// Execute the request
$response = curl_exec($ch);
I am doing something wrong someone can give me a hand? Thanks a lot.
Is there anyone out there who is good with the PHP cURL functions and could possibly translate the following cURL call into the PHP functions?
I'm running into trouble replicating the "--data-binary #/var/www/html/images/request.png" portion into the various PHP options for cURL.
When I execute this on CLI, it works perfectly
curl -X POST --data-binary #/var/www/html/images/request.png http://127.0.0.1:4212/index/searcher
Here's my best try so far but it results in a corrupt image being uploaded:
<?php
$url = 'http://127.0.0.1:4212/index/searcher';
$header = array('Content-Type: multipart/form-data');
$fields = array('file' => '#/var/www/html/images/request.png');
$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, $url);
curl_setopt($resource, CURLOPT_HTTPHEADER, $header);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($resource, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($resource, CURLOPT_POST, 1);
curl_setopt($resource, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($resource) ;
print_r($result);
curl_close($resource);
?>
Interesting part is the file in my system is 225873 bytes whereas after upload it becomes 226062.
Recently I created my owncloud server and I need to be able to upload a file from a php form which transfer an file from my pc to my owncloud server. So I tried to use Curl, like this :
<?php
$url = "5.25.9.14/remote.php/webdav/plus.png";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); // -X PUT
curl_setopt($ch, CURLOPT_USERPWD, "root:root"); // --user
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); // --data-binary
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'img/plus.png' => '#'.realpath('img/plus.png')
)
);
$output = curl_exec($ch);
curl_close($ch);
?>
I have been inspire by this post and this command :
curl -X PUT "http://server.com/owncloud/remote.php/webdav/file.zip" --data-binary #"/Users/Root/Downloads/file.zip"
The command line, he's working but not my php. I succeed to upload the file but the file is corrupted and I don't know why :/. Maybe I miss the MIME type ? Is it enough to get a corrupted file ?
Do you see where I am wrong ?
Best regards, Zed13
Edit : When I make an file of my uploaded file, it's of type data and not png, strange...
I was having an issue with an upload to owncloud as well. Had the same symptoms, the command line curl works, but not the PHP curl call.
Thanks to your post I was able to get it working. Here is what works for me
// upload backup
$file_path_str = '/tmp/' . date('Ymd') . '.tar.gz';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://server/remote.php/webdav/backups/' . basename($file_path_str));
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
curl_setopt($ch, CURLOPT_PUT, 1);
$fh_res = fopen($file_path_str, 'r');
curl_setopt($ch, CURLOPT_INFILE, $fh_res);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path_str));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); // --data-binary
$curl_response_res = curl_exec ($ch);
fclose($fh_res);
The differences are:
CURLOPT_PUT instead of CURLOPT_CUSTOMREQUEST
CURLOPT_INFILE and CURLOPT_INFILESIZE instead of CURLOPT_POSTFIELDS
Thanks for your help.
//
I've been trying for a while now to call a REST API, but still with no luck.
I've tested the connection, propreties and credentials with WizTools, so I'm 100% all the data is correct and working. Only when I try to connect to the API using custom PHP, things go wrong.
I use Fiddler for debugging and all I see is a request to the local path of the file with the code. For some reason, the snippet isn't calling the REST endpoint... I would like to know how does it come and what do I do wrong...
The code is hosted local (dev.testing.com).
At first sight, I would think the code below is correct, so I'm wondering why I get a GET http://dev.testing.com/talent.php HTTP/1.1 result in Fiddler, while I should get a POST https://api.some_url.com/package/REST/service/criteria?api_key=my_Key HTTP/1.1..
<?php
// Set all the data
$service_url = "https://api.some_url.com/package/REST/service/criteria?api_key=my_Key";
$service_user = 'iiii_My_Username:text:FO';
$service_pass = 'password';
// Initialize the cURL
$ch = curl_init($service_url);
// Set service authentication
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, "{$service_user}:{$service_pass}");
// Composing the HTTP headers
$body = "<searchCriteriaSorting></searchCriteriaSorting>";
$headers = array();
$headers[] = 'Accept: application/xml';
$headers[] = 'Content-Type: application/xml; charset=UTF-8';
// Set the cURL options
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
//WARNING: this would prevent curl from detecting a 'man in the middle' attack
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
// Execute the cURL
$data = curl_exec($ch);
// Print the result
echo $data;
?>
I'm using the Amazon AIMS API to upload a an inventory file and I'm having an issue with the cURL call to upload the file. The documentation is very limited, so there is no example code that helps out in this.
This is what I have so far of the cURL call:
// $FILENAME is filename of the CSV file being uploaded:
$inventory = fopen($FILENAME, 'r') or die("Can't open file!");
echo $inventory;
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_INFILE, $inventory);
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filename));
curl_setopt($ch, CUROPT_PUT, TRUE);
$response = curl_exec($ch);
curl_close($ch);
I've try putting $inventory into the CURLOPT_POSTFIELDS and not having an INFILE, but I get the same error.
On the XML response, I'm getting "NO_FILE_ATTACHED" so the obvious issue is getting the file to be attached to the XML call.
I also tried uploading as the first responder said using the Example #2 on the curl_setopt page on php.net.
For that, I used the following code:
$data = array('#/tmp/amazon_export.csv');
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);
I got the same NO_FILE_ATTACHED response back.
Any ideas?
This works for me:
$hCurl = curl_init();
curl_setopt($hCurl, CURLOPT_PUT, true);
curl_setopt($hCurl, CURLOPT_HEADER, true);
curl_setopt($hCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($hCurl, CURLOPT_CONNECTTIMEOUT, CURL_TIMEOUT_SECS);
curl_setopt($hCurl, CURLOPT_URL, "$oMessage->url/att/$fid");
curl_setopt($hCurl, CURLOPT_HTTPHEADER, $aCurlHeaders);
// TODO it could be possible that fopen() would return an invalid handle or not work altogether. Should handle that
$fp = fopen ($finfo['tmp_name'], "r");
curl_setopt($hCurl, CURLOPT_INFILE, $fp);
curl_setopt($hCurl, CURLOPT_INFILESIZE, $finfo['size']);
$sResp = curl_exec($hCurl);
You are combining PUT and POST in a single curl operation, which will not work. Refer to example #2 of the curl_setopt manual page for an example on how to upload a file using POST.
you have $filename and $FILENAME, the filesize call should be in your case filesize($FILENAME)...
Hope thats helps