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:
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:
creating a new file or opening an existing one and then pass this file handler to the curl_set_opt
function.
Hope this will help.!
Regards
Gaurav :)
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:
<?php
$ch = curl_init(); //Initialize the cURL session
//Set the URL of the page or file to download.
curl_setopt($ch,CURLOPT_URL,
'http://news.google.co.in/news?pz=1&cf=all&ned=in&hl=en&topic=t&output=rss' );
// 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:
<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,'http://news.google.co.in/news?pz=1&cf=all&ned=in&hl=en&topic=t&output=rss'); $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); fclose($fp); ?>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
function.
Hope this will help.!
Regards
Gaurav :)
Can this be used in magento modules, or do we have to use
ReplyDelete$http = new Varien_Http_Client($url);
$http->setHeaders( array( "Accept-encoding" => "identity" ) );
if($method=='POST')
$http->setParameterPost($postParams);
$response = $http->request($method);