PHP cURL

December 22, 2009 No comments yet

cURL is a library written in C that enables easy transfer of data in many different protocols including FTP, FTPS, HTTP, TELNET and LDAP. cURL does more than simply download a file. You can store cookies, upload files, use various types of authentication and tunnel all requests through a proxy. The cURL extension has been bundled with PHP since version 4.0.2 and is enabled by default in php.ini. In this tutorial we will look at the uses of cURL and how it compares to file_get_contents() , we will then write a cURL class which we can use to download the source code of a webpage and download a binary file.

cURL vs file_get_contents()

Many PHP scripts require the user to download and then parse the HTML source of a page. When people ask how to do this, the most common reply is to use the file_get_contents() function as it’s a simple one line solution – you just need to set the first argument to webpage you want to download and it will return the source code of it. While it may seem very easy to use, it has many pitfalls.

  1. cURL is significantly faster than file_get_contents() at retrieving the