PHP curl for PUT request to send video file to vimeo - php

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

Related

How can I download the .zip of my private github repository using cURL on php?

I want to download the latest zip version of a private github repository I'm working on, and I want to do this using a PHP script. However, my current PHP script is just returning "Not Found" - I'm guessing I have an issue with my cURL user/pass setup, but I can't figure it out. My current code is as follows:
$username='XXX';
$password='XXX';
$URL='https://github.com/[user]/[reponame]/archive/master.zip';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result=curl_exec ($ch);
file_put_contents('master.zip', $result);
curl_close ($ch);
I was able to get it to work by separating the login page and the file download page into two requests. The following code worked for me:
$username='XXX';
$password='XXX';
$URL='https://github.com/[user]/[reponame]/archive/master.zip';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://github.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result=curl_exec ($ch);
curl_close ($ch);
file_put_contents('master.zip', $result);
I know that this question is a bit older, but it's worth noting that that GitHub allows for the generation of OAuth keys that allow you to download private repos. This allows you to not keep your username and password in your code.
For future seekers, below is working sample code utilising the OAuth access token generated.
The question was for zipball functionality, however the API allows for tarball as well.
$fp = fopen('/path/to/yourfile.zip', 'w+');
$giturl = 'https://api.github.com/repos/{username}/{reponame}/zipball/master?access_token={YourTokenHere}';
$ch = curl_init($giturl);
//set file to write to
curl_setopt($ch, CURLOPT_FILE, $fp);
//the API will not allow you to download without a user agent so CURLOPT_USERAGENT is important.
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP/'.phpversion('tidy'));
//The API URL redirects so the following line is very important
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$output = curl_exec($ch);
if(curl_errno($ch)){
throw new Exception(curl_error($ch));
}
//Get the HTTP status code.
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//Close the cURL handler.
curl_close($ch);
//Output result
if($statusCode == 200){
echo 'Downloaded Successfully';
} else{
echo "Failed downloading - Status Code: " . $statusCode;
}
In addition to your answer I would suggest using the following if you want to download larger files to save memory.
$fp = fopen ('master.zip', 'w+');
curl_setopt($ch, CURLOPT_FILE, $fp);

Sending a file via PHP CURL to remote server

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?

Is it necessary to have base64_encode

I'm using the following tutorial to upload an image to imgur. I want to know whether it's necessary to have base64_encode when sending the data.
Here's a small code snippet:
$img=$_FILES['img'];
$filename = $img['tmp_name'];
$data = fread($handle, filesize($filename));
$pvars = array('image' => base64_encode($data)); // Here's the base64_encode
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
Is it necessary to have base64_encode and why?
To see the full code, you can go here.
According to the Imgur documentation, there are alternative methods.
So, to answer your question, no it is not required as long as it is one of the other two supported options.
No it´s not necessary, and I don't know if this is a common practice for posting huge images.
You can also use the cURL option CURLOPT_INFILE
BEWARE: The below examples, may not work, they are just untestet examples.
// This is the entire file that was uploaded to a temp location.
$localFile = $_FILES[$fileKey]['tmp_name'];
$fp = fopen($localFile, 'r');
// Connecting to website.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url );
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localFile));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
Or just a simple POST
$post = array('extra_info' => '123456','file_contents'=>'#'.$_FILES['upload']['tmp_name']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;

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

How can I attach a file with a PHP cURL XML Call

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

Categories