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.
//
Related
I'm trying to send a file using PHP's CURL functions.
$postdata = curl_file_create(realpath($filename), 'text/csv', $filename);
and then
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
$result = curl_exec($ch);
$cinfo = curl_getinfo($ch);
$cerror = curl_error($ch);
var_dump($result);
var_dump($cinfo);
var_dump($cerror);
curl_close($ch);
This only dumps bool(false), so I'm not getting far with the debugging. Am I POSTing the filename as a string rather than the file contents? The php script and the file to be send are in the same directory. Do I need to specify the path in a certain format?
Update:
I added curl_error($ch) and figured out that the problem is a connection timeout. When running the script from my local machine it seems to be working, at least I get a 200 OK reply. Could my webhost somehow be blocking the connection?
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
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.
I am trying to convert the following Curl command:
curl --digest --user "username:password" --verbose --url "http://127.0.0.1/ws?graph-uri=http://localhost/dataset/import/" -X POST -T /data/datasets/foo.n3
Here is the code that appears to be ok, but that doesn't work:
$args = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1/ws?graph-uri=http://localhost/dataset/import/');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
$args['graph-uri'] = curl_file_create('/data/datasets/foo.n3');
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$result = curl_exec($ch);
It seems that the file is actually not uploaded with the PHP code, but everywhere I look the usage of curl_file_create() seems good.
Try this previous answer, substituting graph-uri for file_contents. Also, you are setting "graph-uri" as both GET and POST params, which could collide, and the graph-uri GET param is not URL encoded, which could behave differently in the shell vs. PHP. So you might try the following:
http://127.0.0.1/ws?graph-uri=http%3A%2F%2Flocalhost%2Fdataset%2Fimport%2F
To upload video to vimeo we have to use put request with binary data as in document .I was trying to send put request from php curl lib
My code is below :
$params = array_merge($params, array(
'client_id' => $this->_consumer_key,
'file_data' => '#'.$file_path // don't include the file in the signature
));
$file_path ="/a.mp4";
$fp = fopen($file_path, 'r');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 86400); // 1 Day Timeout
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path));
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$rsp=curl_exec ($ch);
if (curl_errno($ch))
print_r( curl_error($ch));
curl_close ($ch);
But It is not showing any error.If I'm printing res it gives "malformed" .Any suggestions ?
A lot of things are missing from this. If you plan on sticking with PHP I recommend you just use the official PHP library : https://github.com/vimeo/vimeo-php-lib
If you still want to do it yourself, you need a couple of things.
Authenticate using OAuth 1.0a - https://developer.vimeo.com/apis/advanced#oauth
Follow the Upload documentation https://developer.vimeo.com/apis/advanced/upload