PHP - Download cURL result - php

I wrote function below:
function download_xfs($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIE, 'login=michael; xfss=08ruiweu4tuhb5xqs8');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$string = curl_exec ($ch);
curl_close ($ch);
// Set headers
}
When I use download_xfs("http://address.com/file.html"); it returns binary of file instead of actual file. Can anyone re-write this code to handle file for download?

Your error is not in downloading a file, PNG is propably downloaded OK, but you propably just echo'ing it to screen. You must set HTTP header proper for this file mime type:
header( 'Content-type: image/png' );
But remember do do not echo any other strings/characters before and after image contents (absolutelly nothing, even invisible BOM info about UTF created by Windows notepad on a begining of a php file). If you want to include a image downloadable by your server in your HTML page, you must create another .php with this header and image contents and include it by:
<img src="image_from_download.php">
if you want to receive plain text, but have a unexpected binary
There can be some unknown solution on remote serverer, that sends something different or redirects you to a image dependly on user agent.
Try to set user agent in cUrl to same as your original browser.
Also, make sure that content served by remote server is not gzipped or chunked (i don't know, that curl unzip it automatically?) by sending proper Accept header in HTTP request.
This problem can be also state-depended, maybe you need some cookies set or be logged in remote web application from where you are retreiving .html
Try to turn off your FOLLOWLOCATION, maybe you received a 30x redirect to a image.

Related

php curl return downloaded content to user while file is still downloading

I want to server begins to download a big file. But while this file is downloading output the file content to the user. I tried this code:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 155000);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch); // get curl response
echo $response;
But this code takes a long time. I want to use curl instead of readfile.
See this answer: Manipulate a string that is 30 million characters long
Modifing the MyStream class should change it enough so that you can just echo the results to the browser. Assuming the browser is already downloading the file, it should just keep downloading it.

Downloading files using GZIP

I have many XML-s and I downloaded using file or file_get_content, but the server administrator told me that through GZIP is more efficient the downloading. My question is how can I include GZIP, because I never did this before, so this solution is really new for me.
You shouldn't need to do any decoding yourself if you use cURL. Just use the basic cURL example code, with the CURLOPT_ENCODING option set to "", and it will automatically request the file using gzip encoding, if the server supports it, and decode it.
Or, if you want the content in a string instead of in a file:
$ch = curl_init("http://www.example.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_ENCODING, ""); // accept any supported encoding
$content = curl_exec($ch);
curl_close($ch);
I've tested this, and it indeed downloads the content in gzipped format and decodes it automatically.
(Also, you should probably include some error handling.)
I don't understand your question.
You say that you downloaded these files - you can't unilaterally enable compression client-side.
OTOH you can control it server-side - and since you've flagged the question as PHP, and it doesn't make any sense for your administrator to recommend compression where you don't have control over the server then I assume this is what you are talking about.
In which case you'd simply do something like:
<?php
ob_start("ob_gzhandler");
...your code for generating the XML goes here
...or maybe this is nothing to do with PHP, and the XML files are static - in which case you'd need to configure your webserver to compress on the fly.
Unless you mean that compression is available on the server and you are fetching data over HTTP using PHP as the client - in which case the server will only compress the data if the client provides an "Accept-Encoding" request header including "gzip". In which case, instead of file_get_contents() you might use:
function gzip_get_contents($url)
{
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content=curl_exec($ch);
curl_close($ch);
return $content;
}
probably curl can get a gzipped file
http://www.php.net/curl
try to use this instead of file_get_contents
edit: tested
curl_setopt($c,CURLOPT_ENCODING,'gzip');
then:
gzdecode($responseContent);
Send a Accept-Encoding: gzip header in your http request and then uncompress the result as shown here:
http://de2.php.net/gzdecode

Downloading files using PHP and cURL

I used this function for downloading SWF files(flash games) sucessfully. When I use this script for one particular site it downloads all games(I told the script to download 4 games from a list) with exact size of 299bytes? I tried downloading these games with Google Chrome and the download is sucessfull. Is there something missing in the CURL functions I use or the download algorithm is not good enough? Any help will be greatly appreciated.
function saveFlash($fullPaths,$folder,$gamenames,$i){
$curl = curl_init($fullPaths[$i]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//Create a new file in the given folder
$fp = fopen($folder."/".$gamenames[$i].".swf", 'w');
if ($fp == FALSE){
echo "File not opened<br>";}
//Ask cURL to write the contents to a file
curl_setopt($curl, CURLOPT_FILE, $fp);
//Execute the cURL session
curl_exec ($curl);
//Close cURL session and file
curl_close ($curl);
fclose($fp);
}
Text editor gives the following
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved here.</p>
<hr>
<address>Apache/2.2.3 (CentOS) Server at freeonlinegames.com Port 80</address>
</body></html>
You'll want to set CURLOPT_FOLLOWLOCATION to allow it to follow the redirects.
You may also want to set a CURLOPT_MAXREDIRS so it doesn't redirect out of control.
That error you're getting is a common way of telling you that no hotlinking is allowed. If you simply want to download the SWF, you need to set the referrer.
curl_setopt($ch, CURLOPT_REFERER, 'http://urlofpagetheswfwasfoundon');
If it still doesn't work after that, you might need to set an appropriate user-agent string.
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 Something Something');
Also, be very sure that you are allowed to do what you are trying to do. Ripping stuff off others' sites is very frowned upon, and usually illegal.

echo + file_get_contents() adds extra line breaks to the output

The following code transfers an image that is created on the fly from a server to a client site using cURL. It stopped working recently and have not been able to find out what the problem is:
// get_image.php
ob_start();
// create a new CURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, 'url/to/image.php');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// set timeouts
set_time_limit(30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// open a stream for writing
$outFile = fopen($fileDestination, 'wb');
curl_setopt($ch, CURLOPT_FILE, $outFile);
// grab file from URL
curl_exec($ch);
fclose($outFile);
// close CURL resource, and free up system resources
curl_close($ch);
ob_end_clean();
//image.php
/*
* Create image based on client site ...
*/
$filePath = 'path/to/image.png'
$imageFile = file_get_contents($filePath);
header("content-type: image/png");
echo $imageFile;
unlink($filePath);
The file get_image.php is located in a client site and calls the file image.php located in my server.
After running this code the image in the client site is about 7 bytes larger than the original, these bytes seem to be line breaks. After debugging for several hours I found out that these bytes are added when I echo $imageFile. If the 7 bytes are manually removed from the resulting image, the image displays correctly.
There are no errors nor exceptions thrown. The image created in the server is created with no issues. The only output in FF is "The image 'url/to/image.php' cannot be displayed, because it contains errors"
I am not sure what is causing this. Help is greatly appreciated.
Onema
UPDATE:
http://files.droplr.com/files/38059844/V5Jd.Screen%20shot%202011-01-12%20at%2012.17.53%20PM.png
http://files.droplr.com/files/38059844/QU4Z.Screen%20shot%202011-01-12%20at%2012.23.37%20PM.png
Some things to check.
That both files are stored without BOMs
That '<?php' are the first five characters and '?>' the last two in both files.
That when you remove the ob_start() and ob_end-clean(() it should show no error messages.
If you put the unlink before the genereation, you can see the genereated file - check it is valid.
You might want to start the practice of leaving the final ?> from the end of your files - it isn't necessary, and can cause problems if there is whitespace and newlines following the php delimiter.

PHP rel=NOFOLLOW

I want to download files, from an website using PHP.
And i want to create an php script to download files without going on their website to download files. I just want to pun their link on my script an download the file automatically.
I try with CURL, but doesn't work.... The link is like this <a rel="nofollow" href="/download-15866-114621.srt"><b>Download</b></a>
the code :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
'http://subtitrari.regielive.ro/download-15866-114621.srt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
echo $contents;
curl_close ($ch);
I get "download failed!" as content, which means they probably have some sort of download protection. The best thing is probably to ask them what you should do (assuming you have their permission to download the file) or stop trying (assuming you don't).
Eitherway, try setting a referer header with CURLOPT_REFERER. Maybe they check that header to see that no-one is hotlinked to the file.

Categories