cURL web page contents that have a password and save to file

Posted by: Alex on July 14, 2013

cURL can be used to grab data, information, or even a whole webpage from a designated URL.

This can be very useful for grabbing information between sites, exporting data and manually retrieving it, without having to use database connections.

If the external website has a password with .htaccess or the like then some extra setup is needed.

With the following example, you choose a text file to grab information from on an external website, and then where to save it to on the local website.
This, of course, doesn’t have to be a TXT file, could be HTML, CSV or any data really.

<?php
$username = 'theUsername'; // Change this to your username
$password = 'thePassword'; // Change this to your password
$location = 'https://yourWebsite.com/LinkToTheFile.txt'; // Change this to the external website location of the data you want to grab


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $location);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password);
curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; da; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11');
$contents = curl_exec($ch);
if ($contents === false) {
	trigger_error('Failed to execute cURL session: ' . curl_error($ch), E_USER_ERROR);
}
$file = 'myOutput.txt'; // Change this to choose where to locally save the contents that was grabbed
// Write the contents back to the file
file_put_contents($file, $contents);

?>

Just make sure to change the first 3 variables, to your relevant details, and also the $file variable, to choose where you want to save the output.