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);
Related
I need to download a file from a webDAV site using PHP. I have tried the code below using cURL but I get a "400 Bad Request" error.
Can someone help me with what I am doing wrong?
$fp = fopen("c:/downloadtest.text", "w");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result = curl_exec ($ch);
curl_close ($ch);
fclose($fp);
I finally got it working, I was not sure how to make the CURLOPT_WRITEFUNCTION option work:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$fp = fopen("c:/downloadtest.text", "w");
fwrite($fp, $output);
fclose($fp);
curl_close($ch);
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 want to upload the image to the url using Curl.
But as the Image file is on https url i am not able to read the file using fopen.
Code is as below.
$file = "https://xyz.com/image.jpg";
$url = "http://abc.com/upload.php";
$fp = fopen($file, "r");
$headers = array("Content-Type: xml");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$responseInfo = curl_getinfo($ch);
curl_close($ch);
I think that this should work assuming that you don't need to pass the file data that you're uploading in as a named key/value pair.
$file = "https://xyz.com/image.jpg";
$url = "http://abc.com/upload.php";
$fileData = file_get_contents($file);
$fp = fopen($file, "r");
$headers = array("Content-Type: xml");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fileData);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$responseInfo = curl_getinfo($ch);
curl_close($ch);
Instead of:
curl_setopt($ch, CURLOPT_PUT, true);
opt setting for PUT API request will work fine on using this setting below:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
I upload file to the remote server. The code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ZDURL.$url);
curl_setopt($ch, CURLOPT_USERPWD, ZDUSER."/token:".ZDAPIKEY);
$params = array('file_name' => '#'.$temp_file);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/plain"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
But in the top of uploaded file appears information:
------------------------------cfbe90a606af
Content-Disposition: form-data; name="file_name"; filename="C:\Program Files\xampp\tmp\phpF576.tmp"
Content-Type: application/octet-stream
images have wrong format owing to this addition text
It sounds like the URL you are sending data to is expecting just a file in the request body, rather than a form submission. The best way to achieve this is with CURLOPT_INFILE. Try this code:
Try this
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ZDURL.$url);
curl_setopt($ch, CURLOPT_USERPWD, ZDUSER."/token:".ZDAPIKEY);
// You need to detect the correct content type for the file you are sending.
// Although based on the current problem you have, it looks like the server
// ignores this anyway
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: image/jpeg"));
// Send the raw file in the body with no other data
$fp = fopen($temp_file, 'r');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
fclose($fp);
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);