php curl basic reference

cURL basics and functions over view

cURL is a PHP library which allows you to connect and communicate to many different types of servers with many different types of protocols.Main difference is it has session capability unlik fopen. We can make small scale robot script with cURL functions in PHP.

With cURL we can:

  • Implement payment gateways’ payment notification scripts.
  • Download and upload files from remote servers.
  • Login to other websites and access members only sections.
  • Cross site communication with PHP backend and Ajax.

PHP cURL library is definitely the odd man out. Unlike other PHP libraries where a whole plethora of functions is made available, PHP cURL wraps up a major parts of its functionality with following functions.

Curl functions are:

  1. curl_close — Close a cURL session
  2. curl_copy_handle — Copy a cURL handle along with all of its preferences
  3. curl_errno — Return the last error number
  4. curl_error — Return a string containing the last error for the current session
  5. curl_exec — Perform a cURL session
  6. curl_getinfo — Get information regarding a specific transfer
  7. curl_init — Initialize a cURL session
  8. curl_multi_add_handle — Add a normal cURL handle to a cURL multi handle
  9. curl_multi_close — Close a set of cURL handles
  10. curl_multi_exec — Run the sub-connections of the current cURL handle
  11. curl_multi_getcontent — Return the content of a cURL handle if CURLOPT_RETURNTRANSFER is set
  12. curl_multi_info_read — Get information about the current transfers
  13. curl_multi_init — Returns a new cURL multi handle
  14. curl_multi_remove_handle — Remove a multi handle from a set of cURL handles
  15. curl_multi_select — Wait for activity on any curl_multi connection
  16. curl_setopt_array — Set multiple options for a cURL transfer
  17. curl_setopt — Set an option for a cURL transfer
  18. curl_version — Gets cURL version information

curl_setopt is the pivot around which the main cURL functionality revolves. cURL functioning is controlled by way of passing predefined options and values to this function.

A simple example to handle google rss feed

 
/*
*
* Initialize the cURL session
*/
$curl_handler = curl_init();
/*
*
* Set the URL of the page or file to download.
*/
curl_setopt($curl_handler, CURLOPT_URL,
'http://news.google.com/news?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($curl_handler, CURLOPT_RETURNTRANSFER, 1);
/*
*
* Execute the cURL session,do some stuff with content
*/
$contents = curl_exec ($curl_handler);
/*
*
* Close cURL session,
*be sure to close the handler as curl stores cookies ,other file data stores on server.
*/
curl_close ($curl_handler);

In the above example we used CURLOPT_URL,CURLOPT_RETURNTRANSFER options.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">