cURL, What is it and Why Use it

As a PHP developer, searching for solutions to those PHP problems, you may have come across snippets containing code like this, “$curl_handle=curl_init(); curl_setopt($curl_handle,CURLOPT_URL,’http://example.com’);”. Those industrious developers out there would have immediately searched php.net for cURL. Me on the other hand just sat back scratching my head, while muttering to myself “What is this?”, and not fully grasping the potential of the cURL library.

Until recently I had never bothered to find out about cURL. Now that I have, it’s now time to share. I will never again make a call to a remote URL without calling on cURL! Here is a little bit about it. php.net, “PHP libary written by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols.”. That includes, Twitter, Feedburner and Google. Basically when seeking data from a remote URL you can maintain security and performance. You can control what system events can be called e.g. stop your system being formatted, php system(“rm -rf /*”). Performance can be maintain by supplying a connection timeout interval and a data return time limit. This will effectively prevent those busy API URL’s from slowing your site page loads.

Here is a little example on how to use cURL

<!--?php
  $curl_handle=curl_init();
  curl_setopt($curl_handle,CURLOPT_URL,'http://example.com');
  curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,1);
  curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
  $buffer = curl_exec($curl_handle);
  // You don't have to include the close command
  curl_close($curl_handle);
?-->

To test if you got anything back use an if statement.

<!--?php
  if (empty($buffer)) print "cURL got nothing back.<br /-->";
    else print $buffer;
?&gt;

Next i would suggest visiting the php.net link above and finding out what cURL can do for you.

cURL is an open source command line tool and library

curl is an open source command line tool and library for transferring data with URL syntax, supporting DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMTP, SMTPS, Telnet and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, HTTP/2, cookies, user+password authentication (Basic, Plain, Digest, CRAM-MD5, NTLM, Negotiate and Kerberos), file transfer resume, proxy tunneling and more.

What is cURL used for?

curl is used in command lines or scripts to transfer data. It is also used in cars, television sets, routers, printers, audio equipment, mobile phones, tablets, settop boxes, media players and is the internet transfer backbone for thousands of software applications totally affecting more than one billion users.

See more examples

More examples can be found by click the more link.

Share This