PHP class for cURL

Posted on November 6, 2008
by

I discovered libcurl a long time ago, but never really worked with… until yesterday! It was during a Solaris course, my classmate showed me some stuff he did with libcurl, and I was amazed of the capabilities! On the evening I jumped onto my macbook and wrote a class to manage cURL requests.

My main utilization is to get quickly, and in an automatic manner, information on my accounts in some websites (such as social networks, affiliation programs or banks – yes, libcurl handle HTTPS!) and why not retrieving all of that in a widget.

Installation

Just download the class file, include it in your project and you’re good to go: cURL class

Usage example

$curl = new Curl(1);
 
// We pass variables in POST
$curl->addPostVar('login','cyril');
$curl->addPostVar('password','foobar');
 
// Connection to facebook, a cookie is created and stored in a file
$curl->exec('http://www.facebook.com/log.php');
 
// Now we can get a page as a logged user
$t = $curl->exec('http://www.facebook.com/profile.php');
 
// And do what we want with the result ...
// generally parsing it with regular expressions

That’s all folks!

About the author

Cyril Mazur is a serial web entrepreneur with experience in various fields: online dating, forex & finance, blogging, online advertising... who enjoys building things that people like to use.

5 comments

  1. Alex
    on December 2, 2011
    Change name of variable from "$r" to "$t" on line 104. It causes permanent looping.
  2. Cyril
    on December 2, 2011
    Oh my god, bien vu Alex. Thanks.
  3. Andrew TUc
    on September 11, 2012
    Thanks a lot MasBro.... :-)
  4. Stefan Kähler
    on February 7, 2013
    Nice Class, works like a charm.

    I changed only one piece of code, because i had a problem with the cookie path.

    On line 52:
    $headers['cookie'] = ROOT.'/tmp/curl/cookies/'.$n;
    to
    $headers['cookie'] = dirname(__FILE__) . "/" . $n;

    This will create the cookie in the directory, where the script is executed. I had some issues on my windows machine and on linux with insufficient read/write permissions
  5. Cyril
    on February 7, 2013
    Thanks Stefan :)

Leave a Reply