Sunday, February 27, 2011

How to use PHP CURL

Below is the sample basic code for using the curl.

<?php

 // create a new cURL resource
 $ch curl_init();
// set URL and other appropriate options

 $options = array(CURLOPT_URL => 'http://www.example.com/',
                 
CURLOPT_HEADER => false
                
);
curl_setopt_array($ch$options);


//curl_setopt($ch,CURLOPT_URL,'http://google.com'); You can use this line rather than the above two lines for initializing the URL.

 // grab URL and pass it to the browser 
curl_exec($ch);
// close cURL resource, and free up system resources

 curl_close($ch);

?>


Below is the explanation of the above functions
  • curl_init method used to initiate curl object.
  • curl_setopt method for setting options to curl object
  • CURLOPT_URL = Curl Option URL, here we need to set the remote server URL
  • curl_exec method will execute the curl object.
  • curl_setopt_array method used to send multiple parameters as an array.
Start make use of this cURL features and feel the joy of it.

No comments:

Post a Comment