maraki2 Posted May 1, 2013 Share Posted May 1, 2013 Hey guys, nice forum here - hope you can help me Here is the situation1. I need to login to an https website using username and password 2. I need to download a zip file which is stored in this https location to my computer's hard drive C: 3. I need to do this automatically once every day any help would be much appreciated as i don't know where to start - I read that the best solution would be to use PHP Curl but need help with the syntax.cheers Maraki Link to comment https://forums.phpfreaks.com/topic/277495-download-file-to-my-hard-drive-at-regular-interval/ Share on other sites More sharing options...
lemmin Posted May 1, 2013 Share Posted May 1, 2013 cURL is definitely the best solution for sending packets over SSL. Here is a place to start: $post = 'username=yourname&password=yourpass'; $curl = curl_init('https://www.securesite.com/login'); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $post); $response = curl_exec($curl); There is a good chance that the end server will be looking for some extra information, possibly in the form of headers. To find out what this information is, use a packet sniffer (like firebug) to find out exactly what information is being sent by the browser. https://getfirebug.com/ http://us2.php.net/manual/en/function.curl-setopt.php Link to comment https://forums.phpfreaks.com/topic/277495-download-file-to-my-hard-drive-at-regular-interval/#findComment-1427562 Share on other sites More sharing options...
maraki2 Posted May 1, 2013 Author Share Posted May 1, 2013 thanks for the info... i am really a newbie when it comes to cURL.. I found this piece of code but i need to make it so that the file is downloaded to my hard drive... function get_stats($login_url, $username, $password, $login_button, $csv_title, $download_url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $login_url); $post_fields = 'username='.urlencode($username).'&password='.urlencode($password); // Some sites don't send this variable, hence the check if(trim(urlencode($login_button)) != ''){ $post_fields .= '&'.$login_button.'='.$login_button; } curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookies.txt'); curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookies.txt'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3"); curl_exec($ch); // Prevent the script from timing out set_time_limit(0); $fp = fopen (dirname(__FILE__) . '/'.$csv_title, 'w+');//This is the file where we save the information curl_setopt($ch, CURLOPT_URL, $download_url);//Here is the file we are downloading curl_setopt($ch, CURLOPT_TIMEOUT, 50); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_exec($ch); curl_close($ch); fclose($fp); } Link to comment https://forums.phpfreaks.com/topic/277495-download-file-to-my-hard-drive-at-regular-interval/#findComment-1427581 Share on other sites More sharing options...
lemmin Posted May 1, 2013 Share Posted May 1, 2013 If you have the connection working, all you have to do is save the response in a local file: $response = curl_exec($ch); file_put_contents(dirname(__FILE__) . '/'.$csv_title, $response); Link to comment https://forums.phpfreaks.com/topic/277495-download-file-to-my-hard-drive-at-regular-interval/#findComment-1427599 Share on other sites More sharing options...
maraki2 Posted May 2, 2013 Author Share Posted May 2, 2013 thanks lemmin, one last question what would be the syntax to fire this function using php... sorry for being such a newb - getting there.. Link to comment https://forums.phpfreaks.com/topic/277495-download-file-to-my-hard-drive-at-regular-interval/#findComment-1427670 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.