Curl Functions used in PHP
Curl Requirements & Installation
======================
Requirements
=========
In order to use PHP’s CURL functions you need to install the libcurl package. PHP requires that you use libcurl 7.0.2-beta or higher. In PHP 4.2.3, you
will need libcurl version 7.9.0 or higher. From PHP 4.3.0, you will need a libcurl version that’s 7.9.8 or higher. PHP 5.0.0 requires a libcurl version 7.10.5 or greater.
Installation
=======
To use PHP’s CURL support you must also compile PHP –with-curl[=DIR] where DIR is the location of the directory containing the lib and include
directories. In the “include” directory there should be a folder named “curl” which should contain the easy.h and curl.h files. There should be a file named libcurl.a
located in the “lib” directory. Beginning with PHP 4.3.0 you can configure PHP to use CURL for URL streams –with-curlwrappers.
Curl Functions
==========
Most Commonly used CURL functions are
-
curl_init() - Initializes a Curl Session.
int curl_init();
- it returns a CURL handle for connection. -
curl_setopt() - here, set all the required options for CURL Transfer.
bool curl_setopt(int ch,string option,mixed value)
- it sets the option for the current CURL Session.here,
curl_exec() - Executes the Curl, it returns the response.
curl_exec(int ch)
- it returns the response of the execution. -
finally, curl_close close the curl session.
curl_close(int ch)
- it close the CURL Session.
Sample Code for Connecting URL using Curl:
===============================
$ch = curl_init(); // Initialilzes a curl session
// Setting options for the current Session
curl_setopt ($ch, CURLOPT_URL,”www.google.com“); // Set the URL
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postfields); // Set the post fields.
curl_setopt ($ch, CURLOPT_RETURNTRANSFER,1);
$response = curl_exec ($ch); // Executing a CURL.
curl_close ($ch); // Close the current session.
==========================================================
