I have function to download remote file based on offset and limit.
function get_part_file($url, $offset, $limit){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RANGE, ''.$offset.'-'.$limit.'');
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
I wanna download 1024 bytes from end but this function download whole file. So, how can I do that?
If the remote host does not support Range headers, it doesn't matter what you do: you won't be able to download a specific range.
$ch = curl_init(trim($remoteFile));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RANGE, '0-102399');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false );
$fp = fopen($filepath, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
fclose($fp);
curl_close($ch);
Related
I want to attach a PDF file to the mail via CURL response.
I have an URL which prompts to download the file woth Ok button (Windows default).
I have tried below code snippets but no luck..
$hCurl = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $verbose = fopen('php://temp', 'rw+'));
curl_setopt($hCurl, CURLOPT_PUT, true);
curl_setopt($hCurl, CURLOPT_HEADER, true);
curl_setopt($hCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($hCurl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($hCurl, CURLOPT_URL, "http://exampe.com/invoice/41/download");
$fp = fopen ($finfo['tmp_name'], "r");
curl_setopt($hCurl, CURLOPT_INFILE, $fp);
curl_setopt($hCurl, CURLOPT_INFILESIZE, $finfo['size']);
$sResp = curl_exec($hCurl);
rewind($verbose);
$debug = "Verbose info:\n<pre>" . htmlspecialchars(stream_get_contents($verbose)) ."</pre>";
echo $finfo['tmp_name'];
fclose($verbose);
curl_close($hCurl);
AND
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
curl_setopt($ch, CURLOPT_URL, "http://exampe.com/invoice/41/download");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$output = curl_exec($ch);
$file = fopen($output, 'r');
$size = filesize($output);
$fildata = fread($file,$size);
curl_close($ch);
And then tried to add response with
$mail->addAttachment($response);
I wonder if i can ever do it or if i am doing anything wrong here?
I am using below code to post a file to third party API -
$post = array('userName' => 'testabc','password'=>'testabc','FILE1'=>'abc.csv','cn'=>'10215');
$fp = fopen("orders/abc.csv", 'r');
$ch = curl_init("https://differentdomain.com/abc.cgi");
curl_setopt($ch, CURLOPT_USERPWD, "myuser:mypwd");
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 86400);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'CURL_callback');
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize("orders/abc.csv"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$suc = curl_exec ($ch);
echo "==>".curl_error($ch);
echo "-->".$suc; die;
It returns with 500 Internal Server Error. Not sure whether the way I am posting parameters are right or wrong.
Any help appreciated.
Thanks.
Upload image using curl
Try this
This code works:
// Create temp file to write to
$fp_tmp = tmpfile();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $name);
curl_setopt($ch, CURLOPT_FILE, $fp_tmp);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_REFERER, 'https://dl.dropboxusercontent.com');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
// Write the files
$fp = fopen($orderfile->getFileLocation(), 'w');
stream_copy_to_stream($fp_tmp, $fp);
This code does not:
// Create temp file to write to
$fp_tmp = tmpfile();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $name);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_REFERER, 'https://dl.dropboxusercontent.com');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $str) use (&$fp_tmp) {
$length = fwrite($fp_tmp, $str);
return $length;
});
$result = curl_exec($ch);
curl_close($ch);
// Write the files
$fp = fopen($orderfile->getFileLocation(), 'w');
stream_copy_to_stream($fp_tmp, $fp);
I am assuming that I cannot pass a stream via the 'use' to a function this way as it fails to copy the data. I guess my question is, how can I write the content of $str to $fp_tmp using CURLOPT_WRITEFUNCTION?
I'm a dummy, I forgot to reset the pointer using fseek().
// Create temp file to write to
$fp_tmp = tmpfile();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $name);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_REFERER, 'https://dl.dropboxusercontent.com');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $str) use (&$fp_tmp) {
$length = fwrite($fp_tmp, $str);
return $length;
});
$result = curl_exec($ch);
curl_close($ch);
// Write the files
$fp = fopen($orderfile->getFileLocation(), 'w');
fseek($fp_tmp, 0);
stream_copy_to_stream($fp_tmp, $fp);
I want to download a image from the url but it is not getting downloaded.Please check out the code and tell me where i am going wrong.
<?php
$ch = curl_init ("http://l1.yimg.com/t/frontpage/cannes_anjelina_60.jpg");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec ($ch);
curl_close ($ch);
$fp = fopen("img.jpg",'w');
fwrite($fp, $rawdata);
fclose($fp);
?>
It's working fine, set proper file permissions to where the image should be saved. In this case it's the same folder where your script is, might want to move it somewhere else like:
// where "images" folder can be written with files
// set permissions to 0755
$fp = fopen("images/img.jpg",'w');
function download_image ($url, $the_filename) {
$cookie = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Debian) Firefox/0.10.1");
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLINFO_EFFECTIVE_URL, true);
curl_setopt($ch, CURLOPT_REFERER, "http://tomakefast.com");
curl_setopt($ch, CURLOPT_POST, false);
$rawdata=curl_exec($ch);
curl_close($ch);
file_put_contents("../somewhere/". $the_filename, $rawdata);
}
If you see any problems, please let me know. This occasionally gives me a 0 byte image file but that could happen for any number of reasons. This could be improved to return false on 0 bytes, maybe even do a basic test to see if indeed an image was downloaded.
you can use this simple code instead if you feel this is better.
$img_file = file_get_contents("http://l1.yimg.com/t/frontpage/cannes_anjelina_60.jpg");
file_put_contents("myImage.jpg", $img_file);
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