Today I was working on a website which will be an affiliate member of another big company with already running a very good website ( i don't want to mention the name of that affiliate program because with this my idea is some how disclosed to the public and i don't want to do so :) ). In that affiliate program I have to download the feeds in .zip form and then importing that in my database. So I needed a script to automatically download, unzip and then import that data in my database. By using my experience and skills and also with the help of Google, I have successfully written a script for this purpose. In this post I am going to share the same,
First part of my script checks that php zip extension is loaded or not.
First part of my script checks that php zip extension is loaded or not.
if (!extension_loaded('zip')) {
dl('zip.so');
}
After that, mention the path of download file and destination folder and rest of the script is as follows,
$target_url = "http://www.example.com/test.zip";
$userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
$file_zip = "downloads/new.zip";
$file_txt = "downloads/".md5(time())."new.txt";
echo "<br>Starting<br>Target_url: $target_url";
echo "<br>Headers stripped out";
// make the cURL request to $target_url
$ch = curl_init();
$fp = fopen("$file_zip", "w");
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FILE, $fp);
$page = curl_exec($ch);
if (!$page) {
echo "<br />cURL error number:" .curl_errno($ch);
echo "<br />cURL error:" . curl_error($ch);
exit;
}
curl_close($ch);
echo "<br>Downloaded file: $target_url";
echo "<br>Saved as file: $file_zip";
echo "<br>About to unzip ...";
// Un zip the file
$zip = new ZipArchive;
if (! $zip) {
echo "<br>Could not make ZipArchive object.";
exit;
}
if($zip->open("$file_zip") != "true") {
echo "<br>Could not open $file_zip";
}
$zip->extractTo("$file_txt");
$zip->close();
echo "<br>Unzipped file to: $file_txt<br><br>";
If you like this script, so please post your comments and feedback. Your good feedback is the main desire of me to continue writing useful posts for my blog reader.
Thanks
Great, I´m going to test it.
ReplyDeleteOk, but for me, it only worked after inserting
ReplyDeletecurl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).DIRECTORY_SEPARATOR.'c00kie.txt');
as explained here http://stackoverflow.com/questions/12400747/curl-returning-blank
best regards.