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;
Related
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);
I am using this script to upload myfile by curl function and ftp connection.
In local it works fine but in my server file is appeared uploaded but it have zero file size.
what is wrong? Thank You.
$ch = curl_init();
$localfile = (dirname(__FILE__).'/asset/myfile.zip');
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, "ftp://$user_name:$user_pass#$server/".'myfile.zip');
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
curl_close ($ch);
and also how can I upload multiple files in this script like:
$localfile1 = (dirname(__FILE__).'/asset/myfile1.zip');
$localfile2 = (dirname(__FILE__).'/asset/myfile2.zip');
$localfile3 = (dirname(__FILE__).'/asset/myfile3.zip');
This source may helps your problem.
http://www.web-development-blog.com/archives/tutorial-ftp-upload-via-curl/
To upload the file in curl you can use the curl_file_create.Try the below one:
$localfile = (dirname(__FILE__).'/asset/myfile.zip');
$curl_file = curl_file_create($localfile,'zip');
$params = ['file' => $curl_file];
$ch = curl_init();
$localfile = (dirname(__FILE__).'/asset/myfile.zip');
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, "ftp://$user_name:$user_pass#$server/".'myfile.zip');
curl_setopt($ch, CURLOPT_UPLOAD, 1);
//curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
curl_close ($ch);
I've been on this for 3 days now but I can't seem to resolve it, the user will input the url and the system will upload it on remote server.. this is what I've done so far
$POST_DATA = array(
'file' => '#'. 'http://images.visitcanberra.com.au/images/canberra_hero_image.jpg',
'extension' => 'txt',
'filename' => 'test',
'directory' => 'uploads/'
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://1234.4321.67.11/upload.php');
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA);
$response = curl_exec($curl);
curl_close ($curl);
Well do it in this way.
First download that file from url provided by user.
Upload that image on http://1234.4321.67.11/upload.php
And delete that file after successfully uploaded on server.
This might help you for uploading image on server using cURL : https://stackoverflow.com/a/15177543/1817160
And this might help you in understanding how to download image from url :
Copy Image from Remote Server Over HTTP
Use the following function to upload the remote image using curl. Call this by passing image url and path to folder to save the image.
function grab_image($url,$saveto){
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$raw=curl_exec($ch);
curl_close ($ch);
if(file_exists($saveto)){
unlink($saveto);
}
$fp = fopen($saveto,'x');
fwrite($fp, $raw);
fclose($fp);
}
and ensure that in php.ini allow_url_fopen is enable
$file_name_with_full_path = realpath('./upload/example.png');
$curl_handle = curl_init("https://example.com");
curl_setopt($curl_handle, CURLOPT_POST, 1);
$args['file'] = new CurlFile($file_name_with_full_path, 'image/png');
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $args);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$returned_data = curl_exec($curl_handle);
curl_close ($curl_handle);
echo $returned_data;
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
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