File Information with cURL
PHP/MySQL
Last Updated: 2007-02-04 08:23:45
Last Updated: 2007-02-04 08:23:45
STEP 1: About cURL
cURL stands for 'Client URL', and is perhaps the most powerful PHP extension available. cURL allows you to communicate with other servers. This may not sound like it's that interesting until you fully understand what you can do with it. For instance you can write internet crawlers that gather information for search engine results (HINT: see my search engine page). You can write merchant account gateways that validate credit card information with your bank in line with your script so there is no refreshing of the page. You can process information on FTP servers. You can gather stock information and weather information from other sites to use on your own. The possibilities are endless.
To use the cURL library you must have cURL installed as an extention to your PHP installation. If you are looking for a web host who already has cURL extentions installed, a good one that I use is fatcow.com. If you do use them be sure to list Joe Fitzgerald as your reference, because they will give me a free month of hosting if you do... and I'd appreciate that.
This tutorial assumes you've been through the first cURL tutorial that you can find here.
To use the cURL library you must have cURL installed as an extention to your PHP installation. If you are looking for a web host who already has cURL extentions installed, a good one that I use is fatcow.com. If you do use them be sure to list Joe Fitzgerald as your reference, because they will give me a free month of hosting if you do... and I'd appreciate that.
This tutorial assumes you've been through the first cURL tutorial that you can find here.
STEP 2: Returning File Information
With cURL you can retrieve useful information about a file such as the size of the file download or upload, size of all header or requests, server errors and more. A complete list can be found on the W3.org web site. To do this we use the cURL function curl_getinfo().
<?php
// Initialize your cURL session
$ch = curl_init();
// Set option for the file you wish cURL to open
curl_setopt($ch, CURLOPT_URL, 'http://www.amazon.com/Ronin-Frank-Miller/dp/0930289218');
// Tell cURL to return the results into the $ch variable as opposed to printing them out
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Execute your cURL script
curl_exec($ch);
// Return the results as an associative array using curl_getinfo()
$info = curl_getinfo($ch);
// Loop through the array that curl_getinfo populated
foreach ($info as $key=>$value) {
print "$key -> $value
";
}
// Free up system resources that cURL is using
curl_close($ch);
?>
<?php
// Initialize your cURL session
$ch = curl_init();
// Set option for the file you wish cURL to open
curl_setopt($ch, CURLOPT_URL, 'http://www.amazon.com/Ronin-Frank-Miller/dp/0930289218');
// Tell cURL to return the results into the $ch variable as opposed to printing them out
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Execute your cURL script
curl_exec($ch);
// Return the results as an associative array using curl_getinfo()
$info = curl_getinfo($ch);
// Loop through the array that curl_getinfo populated
foreach ($info as $key=>$value) {
print "$key -> $value
";
}
// Free up system resources that cURL is using
curl_close($ch);
?>
This is the JoeLucky39 tutorial section. Feel free to use whatever information you find here on your own site(s). Know that not all tutorials are ideal for all sites, so feel free to modify the information contained in this tutorail section as best suits your web site.
At JoeLucky39 you can find tutorials on Comic Art, PHP/MySQL, and more are added all the time. So, visit often and feel free to contact Joe if you think that his tutorials need to be changed or updated.
Joe believes in the open source movement and is happy to share his knowledge with anyone who asks. All Joe asks is, if you use Joe's open source tutorials, you share your knowledge as well.
Thank you and I hope you find what you are looking for. If not feel free to contact me and ask.
