Translate the curl with data binary to php - php

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.

Related

Not able to upload file using curl in PHP + Windows | but working in Ubuntu

The following piece of code, uploads the files fine on Ubuntu/Mac but uploads this kind of thing, when I try to run this code on Windows.
--------------------------56a03f9b502371ba
Content-Disposition: form-data; name="file"
#C:\Users\Eckovation\Desktop\test.archive
--------------------------56a03f9b502371ba--
The code that I used is as follows,
$path="C:\\Users\\AK\\Desktop\\test.archive";
$header = array('Content-Type: multipart/form-data');
$fields = array('file' => '#' . "$path");
$resource = curl_init();
curl_setopt($resource, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($resource, CURLOPT_URL, $url);
curl_setopt($resource, CURLOPT_HTTPHEADER, $header);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, false);
curl_setopt($resource, CURLOPT_POST, 0);
curl_setopt($resource, CURLOPT_POSTFIELDS, $fields);
$result = json_decode(curl_exec($resource));
curl_close($resource);
return $result;
In here, the URL in question is a signed url of AWS S3 object.
The url has absolutely no problems, as same code works fine on Ubuntu/Mac.

get robots.txt with curl in PHP

I am trying to execute a robots.txt HTTP request. For that I am using PHP and curl. This is the code I tried:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "robots.txt");
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: www.stackoverflow.com'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
echo $result;
Nothing gets printed to the screen. Any idea what am I missing?
In CURLOPT_URL you must write full host and path
For example: http://www.stackoverflow.com/robots.txt

updating FIREBASE over PHP fails

I have a problem, I tried updating JSON formated data with CURL to my online database: Firebase. It works fine at first, but when I send Data witch I have sent somewhen before it doesn´t get stored. I am sending a combination of a few GET-Variables.
if($_GET['user']!='')
{
$user_url = str_replace('.' , ',' , $_GET['user']);
$url='https://websitenew.firebaseio.com/' . $user_url . '.json';
$data = array('author'=>$_GET['user'], 'temp'=>$_GET['temp'], 'druck'=>$_GET['press'], 'notMoving'=>$_GET['move'], 'gpsx'=>$_GET['gpsx'], 'gpsy'=>$_GET['gpsy']);
$data_json = json_encode($data);
$header = array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_json));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_exec($ch);
curl_close($ch);
}
Thanks!
This should Ideally work with get Variable. If that does not work then try mapping it to a variable in php and then creating a JSON out of it. If the API does not work. Please try out the below one which I had used in my project and it is fully functioning too.
https://github.com/eldhosemjoy/firebase
I hope this git could help you understand the operations with firebase too.

What is Php's equivalent of Java's newInputStreamSupplier?

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.

Upload a file on my Owncloud server with PHP

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.
//

Categories