Using Curl to access remote content

Published by

Posted on February 20, 2008

Description
cURL is a command line tool for transferring files with URL syntax, supporting FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, FILE and LDAP. cURL supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, kerberos…), file transfer resume, proxy tunneling and other useful tricks.

Examples

Fetching a web page

Alternative for file_get_contents()
Instead of:


Use this:


Otherwise if you are getting some errors with the code above, use this:

Getting binary data

Images
This script retrieves a remote image and assigns the binary data to the variable $image, before outputting the image to the browser:

Alternative for file()
Instead of:

$line) {
echo “Line # {$line_num} : “.htmlspecialchars($line).”
\n”;
}
?>
Use this:

$line) {
echo “Line # {$line_num} : “.htmlspecialchars($line).”
\n”;
}
?>

Wrapping it all in an easy class
Use the following class to make reading/saving remote files easy. This class will automatically delete the temp files downloaded at the end of your PHP script.

tempFiles as $file) {
unlink($file[‘temp’]);
}
}
function __construct ($temp)
{
$this->tempFolder = $temp;
}
function get ($url) {
array_unshift($this->tempFiles, array(
‘extension’=> array_pop(explode(‘.’, $url)),
‘original’=> basename($url),
‘temp’=> $this->tempFolder . md5(microtime()),
));
$ch = curl_init($url);
$fp = fopen($this->tempFiles[0][‘temp’], ‘w’);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_exec($ch);
curl_close($ch);
fclose($fp);
return $this->tempFiles[0][‘temp’];
}
function read ($index = 0) {
return file_get_contents($this->tempFiles[$index][‘temp’]);
}
function readArray ($index = 0)
{
return file($this->tempFiles[$index][‘temp’]);
}
function listFiles () {
return $this->tempFiles;
}
function save ($path, $index = 0) {
copy($this->tempFiles[$index][‘temp’], (is_dir($path) ? $path . $this->tempFiles[$index][‘original’] : $path));
}
}
$d = new downloader(‘/home//‘);
?>