save remote (dynamic generated) PDF to server - php

I would like to copy a PDF from a URL (API) to our server with PHP.
When I call the URL in the browser, the file start to download directly, so this ain't a static PDF/URL. I think that's problem.
I've tried different functions with PHP but with no luck:
file_put_contents, copy, fopen/write.
Could you please advise?
For example i've tried:
$url_label = "http://example.com/Public/downloadlabel.aspx?username=$username&password=$password&layout=label10x15&zipcode=$zipcode&shipment=$ShipmentID";
file_put_contents("label.pdf", fopen($url_label, 'r'));
The PDF-file is created in my folder, but this file is empty (0 bytes).
And with curl I hoped to pass the forced download:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_label);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
$data = curl_exec($ch);
curl_close($ch);
$destination = dirname(__FILE__) . '/file.pdf';
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);
curl_close($ch);
The PDF-file is created with 277 bytes, but is corrupted.
Any ideas?

Related

cURL request is not using the CURLOPT_ENCODING flag

I'm trying to download a csv that's been gz'd directly to a file:
$fp = fopen ($file.'.csv', 'w+');
$ch = curl_init($url.'/'.$file.'.csv.gz');
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_exec($ch);
curl_close($ch);
fclose($fp);
What ends up happening is {$file}.csv is written to disk, however it's still encoded. If I rename the stored file {$file}.csv.gz and gunzip it the data is decoded properly.
I ended up downloading the file and creating another block of code to decode it afterwards.
$buffer_size = 262144;
$in_file_handle = gzopen($symbol.'.csv.gz', 'rb');
$out_file_handle = fopen($symbol.'.csv', 'wb');
// Keep repeating until the end of the input file
while(!gzeof($in_file_handle)) {
fwrite($out_file_handle, gzread($in_file_handle, $buffer_size));
}
// Files are done, close files
fclose($out_file_handle);
gzclose($in_file_handle);
I would however, still be very much interested in getting curl to do it all in one step.

Broken images after download using php curl

I have a class that allows me to download images using php curl. My class looks like this:
function getImage($img, $path) {
$fullpath = basename($img);
$ch = curl_init($img);
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);
if(file_exists($fullpath)) {
unlink($fullpath);
}
$fp = fopen($path.$fullpath, "w+");
fwrite($fp, $rawData);
fclose($fp);
}
This works okay for most images but there are also instances in which I get broken images instead. I've tried checking the path of the image from the website and it's correct. My question is, why is this happening and how can I prevent images being downloaded broken?

How to download a file using php, Provided path is known

I've to download the pdf files related to the data from the web source. I know the full path of the file. I've tried with curl but it is taking long time and writing a 0 byte file.
$ch = curl_init ($url);
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);
if(file_exists($fullpath)){
unlink($fullpath);
}
$fp = fopen($fullpath,'x');
fwrite($fp, $rawdata);
fclose($fp);
$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
http://www.php.net/manual/en/curl.examples-basic.php
Or with this (if fopen wrappers are set up in your PHP conf):
$file = 'http://somehosted.com/file.pdf'; // URL to the file
$contents = file_get_contents($file); // read the remote file
touch('somelocal.pdf'); // create a local EMPTY copy
file_put_contents('somelocal.pdf', $contents); // put the fetchted data into the newly created file
// done :)
And this one might fit you the best: http://www.jonasjohn.de/snippets/php/curl-example.htm
It's hard to say without seeing what your code looks like and where you might be going wrong, but take a look at this and see if there's anything that stands out as something you might have overlooked:
http://davidwalsh.name/download-urls-content-php-curl

Image corrupted after php curl transfer FTP

I am using the following code to transfer an image and it is working except the jpg is corrupted after the transfer. Is says invalid image format and shows a blurred image.
I tried using regular php without curl and get the same results.
Does anyone know why whatever I try works but corrupts the image.jpg
$curl = curl_init();
$fh = fopen("test.jpg", 'w');
curl_setopt($curl, CURLOPT_URL, "ftp://{$serverInfo['user']}: {$servererInfo['password']}#{$serverInfo['ftp1.server.com']}/{$serverInfo['For_Web/Web Images/Full Size/00-99/file']}");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
fwrite($fh, $result);
fclose($fh);
curl_close($curl);`
There are a few problems;
You should open your file for writing in binary mode, that is;
$fh = fopen("test.jpg", 'wb');
curl_exec returns a bool (success), not the contents of the file, the file should instead be passed to CURLOPT_FILE.
You should set the username/password using CURLOPT_USERPWD, not sure if the URL way could be made to work too, though.
You should set CURLOPT_BINARYTRANSFER.
Working sample;
$curl = curl_init();
$fh = fopen("fips.exe", 'wb');
curl_setopt($curl, CURLOPT_URL, 'ftp://ftp.sunet.se/pub/FreeBSD/tools/fips.exe');
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FILE, $fh);
curl_setopt($curl, CURLOPT_USERPWD, 'anonymous:olle');
$result = curl_exec($curl);
fclose($fh);
curl_close($curl);

Incorrect MD5 checksum after downloading with CURL in PHP - file_get_contents works fine

I have a script where I have to download some files and to make sure that everything worked fine I'm comparing MD5 checksums.
I have found that the checksums are not correct when downloading with CURL. The script below demonstrates this. It downloads the Google logo and compares checksums.
$url = 'http://www.google.com/intl/en_ALL/images/logo.gif';
echo md5_file($url)."\n";
$path = 'f1';
file_put_contents($path, file_get_contents($url));
echo md5_file($path)."\n";
$path = 'f2';
$out = fopen($path, 'wb');
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_exec($ch);
curl_close($ch);
echo md5_file($path)."\n";
the output is:
e80d1c59a673f560785784fb1ac10959
e80d1c59a673f560785784fb1ac10959
d83892759d58a1281e3f3bc7503159b5
The first two are correct (they match the MD5 checksum when I download the logo using firefox) and the result produced by curl is not OK.
any ideas how to fix that?
thanks for your help
UPDATE:
interestingly the code below works just fine and produces the correct output. The problem really only seems to exist when saving to a file. Unfortunately I have to save directly to a file since the files I'm downloading can get rather large.
$path = 'f3';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
file_put_contents($path, curl_exec ($ch));
echo md5_file($path)."\n";
curl_close ($ch);
You're missing an fclose($out), which could account for md5_file seeing an incomplete file.
Try to add
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
To your curl options

Categories