PHP 4.0.2 onwards libcurl support was added in PHP, libcurl is a free client library initially written by Daniel Stenberg which allows us to communicate with servers using a variety of protocols like HTTP,HTTPS,FTP,SMTP,LDAP, etc. In this article we'll be looking at enabling libcurl support in PHP and using it for accessing URLs with HTTP/HTTPs, I'll try to discuss interesting uses of other protocols in some other article soon.
In order to use PHP cURL you need to have libcurl installed on your system. If you are compiling your PHP installation, compile PHP --with-curl[=DIR] where DIR is the directory containing the library source code.
If your using a Debian/Ubuntu system, this should be enough:
In case you are using Windows/XAMPP, just uncomment this line in the php.ini file.
After installing, restart webserver.
You can set various parameters for cURL, like headers, content type, etc. You can specify post fields, cookies, all that can be configured can be done with cURL.
In the example below, we'll just call a URL.
Now, it might happen we want the output of the URL we just invoked, or in other words fetch the contents of the URL.
Now, let us look at some advanced configuration options like setting cookies, specifying cookie jars, POSTing, following location, etc.
Now, that's easy, isn't it? For more options that are available refer to http://php.net/manual/en/function.curl-setopt.php
Installing/Enabling cURL
In order to use PHP cURL you need to have libcurl installed on your system. If you are compiling your PHP installation, compile PHP --with-curl[=DIR] where DIR is the directory containing the library source code.
If your using a Debian/Ubuntu system, this should be enough:
Code:
$ apt-get install php5-curl
Code:
;extension=php_curl.dll
Basic Usage
You can set various parameters for cURL, like headers, content type, etc. You can specify post fields, cookies, all that can be configured can be done with cURL.
In the example below, we'll just call a URL.
PHP Code:
<?
$curl_handler = curl_init();
// set options
curl_setopt($curl_handle,CURLOPT_URL,'http://www.go4expert.com?run=cron');
// execute
curl_exec($curl_handle);
// finally close & free resources
curl_close($curl_handle);
?>
PHP Code:
<?
$curl_handler = curl_init();
// set options
curl_setopt($curl_handle,CURLOPT_URL,'http://www.go4expert.com?format=json');
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
// execute
$content = curl_exec($curl_handle);
// finally close & free resources
curl_close($curl_handle);
[URL="http://www.php.net/print"]print[/URL] $content;
?>
Advanced Usage
Now, let us look at some advanced configuration options like setting cookies, specifying cookie jars, POSTing, following location, etc.
PHP Code:
<?
$curl_handle = curl_init();
// set options
curl_setopt($curl_handle, CURLOPT_URL,'http://www.go4expert.com?format=json&report_id=1');
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl_handle, CURLOPT_COOKIEJAR, '/tmp/cookie_reports.txt');
curl_setopt($curl_handle, CURLOPT_COOKIEFILE, '/tmp/cookie_reports.txt');
// you may manually specify cookies
curl_setopt($curl_handle, CURLOPT_COOKIE, 'user=Anjali;');
// you can ask libcurl to follow 3xx redirects
curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true);
// set post fields
$post_data = [URL="http://www.php.net/array"]array[/URL](
'date_start' => '2012-11-11',
'date_end' => '2012-12-12',
'columns' => 'ip,count,date'
);
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $data);
?>
