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?
- It is a library created by Daniel Stenberg.
- It allows you to connect and communicate to many different types of servers with many different types of protocols.
- 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:
<?php
$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);
?>
- First initialize a cURL session using the curl_init()
- Set all your options for the transfer via the curl_setopt()
- Now you can execute the session with the curl_exec()
- Finally finish off your session using the curl_close().
For more information: please see PHP Manual.
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.
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 !
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.
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.
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
September 8, 2009 at 5:48 am
Hi spyderwebtech,
I have trouble to make automate posts on facebook, myspace and other websites.
Please can you tell me about something about that?
thanks in advance.