PHP cURL functions

cURL is a library which allows you to connect and communicate to many different types of servers with many different types of protocols. it supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols.

A typical PHP cURL, follows the following sequesnce:
curl_init – Initializes the session and returns a cURL handle which can be passed to other cURL functions.

curl_opt – This is the main work horse of cURL library. This function is called multiple times and specifies what we want the cURL library to do.

curl_close – Closes the current cURL sessio    
curl_exec – Executes a cURL session.

Download file or web page using PHP cURL:

$ch = curl_init(); //Initialize the cURL session 
//Set the URL of the page or file to download.
'' );
// Ask cURL to return the contents in a variable instead of simply echoing them to the browser.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$contents = curl_exec ($ch); //Execute the cURL session
print_r ($contents) // print the result

curl_close ($ch); //Close cURL session

As you can see, curl_setopt is used to Set an option for a cURL transfer.
The above code uses two such options.

CURLOPT_URL: Use it to specify the URL which you want to process. This could be the URL of  the         file you want to download or it could be the URL of the script to which you want to post some data.

CURLOPT_RETURNTRANSFER: Setting this option to 1 will cause the curl_exec function to return the     contents instead of echoing them to the browser.

Download file or web page using PHP cURL and save it to file:

$ch = curl_init();


$fp = fopen('rss.xml', 'w'); //Create a new file
curl_setopt($ch, CURLOPT_FILE, $fp); // Ask cURL to write the contents to a file

curl_exec ($ch);
curl_close ($ch);
here i used another function of cURL options that is CUTLOPT_FILE :- which Obtain a file handler by
creating a new file or opening an existing one and then pass this file handler to the curl_set_opt

Hope this will help.!

Gaurav :)


  1. Can this be used in magento modules, or do we have to use

    $http = new Varien_Http_Client($url);
    $http->setHeaders( array( "Accept-encoding" => "identity" ) );
    $response = $http->request($method);


Post a Comment

Popular posts from this blog

Magento Reindexing problem - take too long time

Magento Navigation menu of CMS pages

Script for to remove the cache - Magento