If you’re a Linux user then you’ve probably used cURL. It’s a powerful tool used for everything from sending email to downloading the latest My Little Pony subtitles. In this article I’ll explain how to use the cURL extension in PHP. The extension offers us the functionality as the console utility in the comfortable world of PHP. I’ll discuss sending GET and POST requests, handling login cookies, and FTP functionality.
Before we begin, make sure you have the extension (and the libcURL library) installed. It’s not installed by default. In most cases it can be installed using your system’s package manager, but barring that you can find instructions in the PHP manual.
How Does it Work?
All cURL requests follow the same basic pattern:
- First we initialize the cURL resource (often abbreviated as ch for “cURL handle”) by calling the
curl_init()
function. - Next we set various options, such as the URL, request method, payload data, etc. Options can be set individually with
curl_setopt()
, or we can pass an array of options tocurl_setopt_array()
. - Then we execute the request by calling
curl_exec()
. - Finally, we free the resource to clear out memory.
DateTime
-, or aDateInterval
-object (or both), which may be the case in an OOP-based application (e.g.$customer->expire->add(DateInterval::createFromDateString('+1 year')) // customer paid for one additional year
) Also note, thatDateInterval
understands ISO8601 for time intervals, butdate()
doesn't. At the end of course: Select the one you better fit your needs :) – KingCrunch Apr 12 '12 at 8:19DateInterval
. – KingCrunch Apr 25 '13 at 14:26