How to Easily Use CURL with PHP
My Simple CURL Wrapper Class
Alot of times i have to use CURL with PHP for getting data from remote sites. Its a wonderfull library which makes alot of things easier for developers who want to interact or get data from remote sites and use them inside their own code. This is where my class comes in handy ![]()
For simpler uses you can get data easily simply by:
$data = implode(”" , file(”http://www.abc.com/page1.html”));
But what to do when you have to login to another site and get data from inside the secure page?
So here is what i do with this class :
include(’curl.class.php’);
$browser = new extractor(true);
$loginpost[’username’] = “xyz”;
$loginpost[’password’] = “xyz”;
$formaction = “http://www.abc.com/login.php”;
$somesecurepage = “http://www.abc.com/myaccount.php”;
$data = $browser->getdata($formaction , $loginpost , “” , true , false);
$mypage = $browser->getdata($somesecurepage , array() , “” , true , true);
and most of the time i end up logging in to the remote site and getting the page data from secure pages.
Here is the what this class has to offer.
Constructor function has 3 parameters :
First parameter is to allow cookie usage or not , true or false.
Second parameter is to set the time out of connections. Integer value in seconds.
Third parameter is to allocate a filename for the storage of the cookie for this session. This can be usefull when you have to carry the login session of the remote site to multiple scripts in your own code. I generally pass the session id to this parameter , this way if i have to use the remote cookie on next pages as well , i can use it easily. You have to create a folder called “cookies” and set its permission to 0777 for this to work correctly. All cookies will be stored there.
The class has some accessable variables as well. They help you use proxies for your remote requests
var $proxyaddr; //— you can set the proxy ip or host to use
var $proxyport; //– proxy port
var $proxyuser; //– proxy username
var $proxypass; //– proxy password
The getdata function lets you actually request the remote url and get its data. The function has these parameters.
First parameter is the url you want to request.
Second parameter is the post data you want to send to the remote url. For login, search etc.
Third parameter is the custom referrer you want to send.
Fourth parameter is wether to set cookie or not.
Fifth parameter is wether to use the cookie made earlier.
Sixth parameter is to send a custom user agent you want to send for the request.
Seventh parameter is rarely used , sometimes even when you are doing every thing right , you dont get the expected results after sending post data. Setting this parameter to true , sends the post data as string to curl instead of sending it as an array.
Another usefull function in the class is the search function. This helps you get specific regions out of the html. For instance if you want to get all the <p> inside the html , you can do this.
$return_array = $browser->search(”<p” , “</p>” , $data);
The last parameter of the search function lets you remove the start and end strings from the returned results. The result from this function is an array.
This class i developed has made alot of things easier for me over the years . This makes alot of things easier for you when using curl with php , saves you a bundle of time.
Hope this helps someone else as well. ![]()
Here is the download link

January 3rd, 2007 at 5:15 am
This class is not not working for crawling the data from expedia . i am trying to get the data from expedia.ca using curl from last five days . Expedia.ca writes the cookies using the javascript.
August 19th, 2008 at 3:04 pm
Hi
I don’t know who you are, but your source is really very useful. I’ve test it and it work very efficiently.
Thank you very much, my friend !