cURL is one of those techniques in PHP which I keep seeing mentioned but have never had the opportunity to actually get to understand. So…no time like the present!
PHP’s clientURL library is its binding for a client-side URL transfer library created by Daniel Stenberg, called libcurl. It allows for easy uploading or downloading of information across domains based on a variety of options set by the implementer prior to execution.
The first step in executing a cURL request is the curl_init() function, which you assign to a variable which will be the handler of the request for all further functions.
$curl_handle = curl_init();
This is followed by a number of calls to the curl_setopt() function, with which you assign different constant options to determine how the cURL call processes. There are a large number of these options to choose from (a list can be found here) but for now here’s a really basic request:
curl_setopt($curl_handle, CURLOPT_URL, 'http://www.anexternaldomain.com'); curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
The first invocation of curl_setopt() states the URL which will be requested, using the CURLOPT_URL parameter. The second invocation states that the data which will be returned by the call should be stored to a variable (the default would be for it to be echoed automatically to the browser window when the call is executed).
Now all that remains is to execute the request:
$storage_var = curl_exec($curl_handle); echo $storage_var;
The curl_exec() function takes the handle as a parameter and (if CURLOPT_RETURNTRANSFER was set to 1, i.e. true) stores it to a variable which can then be echoed to the browser or further manipulated.