Accessing password-protected pages with cURL

PHP/MySQL
Last Updated: 2007-02-03 14:20:23
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.

STEP 2: The Script
If you come across an instance where you would need to provide a remote server with a username and password when accessing files, there is a simple and easy way to do that with cURL using the curl_setopt() function with CURLOPT_USERPWD option.

<?php

// initialize your cURL session
$ch = curl_init();

// Set the URL, with the page in which you would like to access
curl_setopt($ch, CURLOPT_URL, 'http://www.amazon.com/Ronin-Frank-Miller/dp/0930289218');

// Username and password, separated by a colon.
curl_setopt($ch, CURLOPT_USERPWD, 'iang:iang');

// Execute your cURL script
curl_exec($ch);

// free up system resources taken by cURL
curl_close($ch);

?>

You can see in the above example that when using CURLOPT_USERPWD you will need to provide your username and password separated by a colon in order for this script to work.

The page from Amazon that this script is accessing does not require a username and password, but if you, for instance, had a script that was accessing bank information such as to make a credit card transaction online, you would definitely need to provide username and password credentials.

Back to Tutorial List

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.
LOG IN:
© Copywrite 2010 JoeLucky39.com