CURL – Client Url Library for PHP


CURL is a very essential library in PHP. In some website, like linkedin.com if you have an account in there you’ll see, there is an option to import contacts from yahoo, gmail or hotmail. If you put your account name and password they will import the contacts from your account.

Do you know how’ll they do so. Yeah, there may be many libraries. But this type of work could be done using the CURL libary. I’ve done this type of works 2 months ago. So, I think I should share it to novice or intermediate PHP developer.

What is CURL?

  1. It is a library created by Daniel Stenberg.
  2. It allows you to connect and communicate to many different types of servers with many different types of protocols.
  3. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols

Before using CURL, must check that it’s installed in server or your localhost. Here, I’ve given a simple example. This example uses the cURL functions to fetch the example.com homepage into a file:

$ch = curl_init('http://www.example.com/');
$fp = fopen('example_homepage.txt', 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
  1. First initialize a cURL session using the curl_init()
  2. Set all your options for the transfer via the curl_setopt()
  3. Now you can execute the session with the curl_exec()
  4. Finally finish off your session using the curl_close().

For more information: please see PHP Manual.

mahmud ahsan

Computer programmer and hobbyist photographer from Bangladesh, lives in Malaysia. My [Business | Twitter | Linkedin | Instagram | Flickr | 500px]

You may also like

6 Comments

  • gf4e
    July 3, 2008 at 5:09 am

    Well, CURL was very useful for me, because I had to organize my script’s work behind a proxy server, I did noit manage to find anything except CURL.

  • junal
    July 5, 2008 at 8:54 pm

    ohh what why you asked me ? he he …nice post. i see you are writing regularly. which is very good thing. Keep it up mahmud !

  • mahmudahsan
    July 6, 2008 at 4:54 am

    Thanks junal vai, actually we should share something those are very essential for novice to intermediate php developer.

  • rubel
    July 9, 2008 at 5:47 pm

    Really Nice post…
    Could you please explain me… How can I save an image in my web server from other website?

    I really like your blog.
    Thank you.

  • spyderwebtech
    February 2, 2009 at 4:20 pm

    CURL is a very cool technology… I have used it to automate posts on Facebook, Myspace, and various other websites

  • saiful103a
    July 9, 2009 at 11:07 pm

    I can’t say i’m intermediate or novice[I think i’m both].But i can say now,what does curl do is maybe clear to me[confused,need to do some example].

LEAVE A COMMENT