Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/articles/php-tutorials/)
-   -   cURL in PHP Examples (http://www.go4expert.com/articles/curl-php-examples-t29344/)

pradeep 12Dec2012 19:27

cURL in PHP Examples
 
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.

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
In case you are using Windows/XAMPP, just uncomment this line in the php.ini file.

Code:

;extension=php_curl.dll
After installing, restart webserver.

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);
?>

Now, it might happen we want the output of the URL we just invoked, or in other words fetch the contents of the URL.
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);
?>

Now, that's easy, isn't it? For more options that are available refer to http://php.net/manual/en/function.curl-setopt.php

jeeva1388 19Jan2013 15:57

Re: cURL in PHP Examples
 
But most of the time, they mean PHP/CURL. In this tutorial, we will first find out what cUrl, libcurl and PHP/CURL are, because we want to make ...


All times are GMT +5.5. The time now is 16:44.