开发者

Downloading files using PHP and cURL

开发者 https://www.devze.com 2023-03-13 03:27 出处:网络
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 e

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 <a href="http://www.freeonlinegames.com/">here</a>.</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.

0

精彩评论

暂无评论...
验证码 换一张
取 消