Bill
84.***.***.***Downloading with cURL - with custom user-agent and referer
http://curl.haxx.se is a very powerful library for working with files (and headers) over the web, and it is included in most popular PHP setups.cURL may be a little intimidating at first for some, so I decided to write a to the point PHP function for basic file retrieval with custom user-agent and referer. It is pretty well commented and I will add PHP code for example uses in posts below.
PHP code:
// Defines the default values for the variables the function can take. Note that the time_out option is in seconds.
function getFile($url, $time_out = 3, $user_agent = FALSE, $referer = FALSE)
{
// Returns false if no URL is provided.
if (!$url)
{
return FALSE;
}
// Initializes cURL.
$ch = curl_init();
// Sets connection options. Use the function call to override default values rather than editing anything here.
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Allows cURL to follow header Location: redirects. Remove or comment out if you do not desire this behaviour.
if (!empty($time_out))
{
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $time_out);
curl_setopt($ch, CURLOPT_TIMEOUT, $time_out);
}
if (!empty($user_agent))
{
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
}
if (!empty($referer))
{
curl_setopt($ch, CURLOPT_REFERER, $referer);
}
// Executes the connection and places the source of the target file in the ['data'] part of the returned array.
$return['data'] = curl_exec($ch);
// Stores all the information we have on the connection (returned HTTP code, etc etc) in the ['info'] part of the returned array.
$return['info'] = curl_getinfo($ch);
// Closes the connection.
curl_close($ch);
// Returns the array we built.
return $return;
}