The PHP Client URL (cURL) library Fulvio Corno e-lite Research Group Dipartimento di Automatica e Informatica Politecnico di Torino Torino - Italy http://elite.polito.it v. 1.0, 2009-03-23
The cURL library License Outline 1 The cURL library 2 / 13 The PHP Client URL (cURL) library
The cURL library License The problem From a PHP page, being able to retrieve information served by another web server The information must be processed by the PHP page, not simply shown on the resulting page We need an synchronous remote call mechanism 3 / 13 The PHP Client URL (cURL) library
The cURL library License Ingredients 1 The URL address of a remote web page 2 The parameters that we should pass to such remote page How many parameters? Names? Data types? Values? In what form? GET? POST? Encoding? 3 The format of the results returned by the remote page Format: text? xml? DTD or schema? Error codes: in http response header or encoded as part of the result? 4 / 13 The PHP Client URL (cURL) library
The cURL library License The cURL library cURL is one of the most powerful PHP extensions. It stands for Client URL, and allows you to communicate with other servers using a wide range of protocols. libcurl (the library behind the PHP cURL extension) currently supports a wide range of protocols, including HTTP, HTTPS, FTP, TELNET, FILE, LDAP, DICT, GOPHER and HTTPS, as well as HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, and user:password authentication. 5 / 13 The PHP Client URL (cURL) library
The cURL library License Main functions curl_init Initialize a cURL session – returns a “handle” object ($ch) curl_setopt Set an option for a cURL transfer – common options: URL, POST data, result disposition curl_exec Perform a cURL session – actual data transfer and http request curl_close Close a cURL session – end of transaction 6 / 13 The PHP Client URL (cURL) library
The cURL library License Basic example $ch = curl_init( ) ; curl_setopt($ch, CURLOPT_URL, "http://abc.com/page.php") ; // do a POST curl_setopt($ch, CURLOPT_POST, true) ; curl_setopt($ch, CURLOPT_POSTFIELDS, "id=333") ; curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ) ; // return the result of curl_exec, instead // of outputting it directly $result = curl_exec($ch) ; curl_close($ch) ; 7 / 13 The PHP Client URL (cURL) library
The cURL library License Sending the request set the URL with CURLOPT_URL parameters in GET encoded in the URL "...page.php?id=333&user=pippo" parameters in POST specify POST instead of GET with option CURLOPT_POST set to true specify all parameters in CURLOPT_POSTFIELDS encoded as a string as an associative array in PHP 8 / 13 The PHP Client URL (cURL) library
The cURL library License Getting the result call curl_exec($ch) usually, the result is directly interpolated in the page (implicit echo) to avoid interpolation, set CURLOPT_RETURNTRANSFER to true result is returned as a string by the curl_exec call 9 / 13 The PHP Client URL (cURL) library
The cURL library License Other functions curl_copy_handle Copy a cURL handle along with all of its preferences curl_errno Return the last error number curl_error Return a string containing the last error for the current session curl_getinfo Get information regarding a specific transfer curl_setopt_array Set multiple options for a cURL transfer curl_version Gets cURL version information 10 / 13 The PHP Client URL (cURL) library
The cURL library License Other options CURLOPT_HEADER TRUE to include the header in the output CURLOPT_PORT An alternative port number to connect to. CURLOPT_HTTPHEADER An array of HTTP header fields to set. CURLOPT_FILE The file that the transfer should be written to. The default is STDOUT (the browser window). 11 / 13 The PHP Client URL (cURL) library
The cURL library License Further information http://www.php.net/manual/en/book.curl.php 12 / 13 The PHP Client URL (cURL) library
The cURL library License License This document is lincensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. http://creativecommons.org/licenses/by-nc-sa/3.0/ 13 / 13 The PHP Client URL (cURL) library

Php client libray

  • 1.
    The PHP ClientURL (cURL) library Fulvio Corno e-lite Research Group Dipartimento di Automatica e Informatica Politecnico di Torino Torino - Italy http://elite.polito.it v. 1.0, 2009-03-23
  • 2.
    The cURL libraryLicense Outline 1 The cURL library 2 / 13 The PHP Client URL (cURL) library
  • 3.
    The cURL libraryLicense The problem From a PHP page, being able to retrieve information served by another web server The information must be processed by the PHP page, not simply shown on the resulting page We need an synchronous remote call mechanism 3 / 13 The PHP Client URL (cURL) library
  • 4.
    The cURL libraryLicense Ingredients 1 The URL address of a remote web page 2 The parameters that we should pass to such remote page How many parameters? Names? Data types? Values? In what form? GET? POST? Encoding? 3 The format of the results returned by the remote page Format: text? xml? DTD or schema? Error codes: in http response header or encoded as part of the result? 4 / 13 The PHP Client URL (cURL) library
  • 5.
    The cURL libraryLicense The cURL library cURL is one of the most powerful PHP extensions. It stands for Client URL, and allows you to communicate with other servers using a wide range of protocols. libcurl (the library behind the PHP cURL extension) currently supports a wide range of protocols, including HTTP, HTTPS, FTP, TELNET, FILE, LDAP, DICT, GOPHER and HTTPS, as well as HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, and user:password authentication. 5 / 13 The PHP Client URL (cURL) library
  • 6.
    The cURL libraryLicense Main functions curl_init Initialize a cURL session – returns a “handle” object ($ch) curl_setopt Set an option for a cURL transfer – common options: URL, POST data, result disposition curl_exec Perform a cURL session – actual data transfer and http request curl_close Close a cURL session – end of transaction 6 / 13 The PHP Client URL (cURL) library
  • 7.
    The cURL libraryLicense Basic example $ch = curl_init( ) ; curl_setopt($ch, CURLOPT_URL, "http://abc.com/page.php") ; // do a POST curl_setopt($ch, CURLOPT_POST, true) ; curl_setopt($ch, CURLOPT_POSTFIELDS, "id=333") ; curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ) ; // return the result of curl_exec, instead // of outputting it directly $result = curl_exec($ch) ; curl_close($ch) ; 7 / 13 The PHP Client URL (cURL) library
  • 8.
    The cURL libraryLicense Sending the request set the URL with CURLOPT_URL parameters in GET encoded in the URL "...page.php?id=333&user=pippo" parameters in POST specify POST instead of GET with option CURLOPT_POST set to true specify all parameters in CURLOPT_POSTFIELDS encoded as a string as an associative array in PHP 8 / 13 The PHP Client URL (cURL) library
  • 9.
    The cURL libraryLicense Getting the result call curl_exec($ch) usually, the result is directly interpolated in the page (implicit echo) to avoid interpolation, set CURLOPT_RETURNTRANSFER to true result is returned as a string by the curl_exec call 9 / 13 The PHP Client URL (cURL) library
  • 10.
    The cURL libraryLicense Other functions curl_copy_handle Copy a cURL handle along with all of its preferences curl_errno Return the last error number curl_error Return a string containing the last error for the current session curl_getinfo Get information regarding a specific transfer curl_setopt_array Set multiple options for a cURL transfer curl_version Gets cURL version information 10 / 13 The PHP Client URL (cURL) library
  • 11.
    The cURL libraryLicense Other options CURLOPT_HEADER TRUE to include the header in the output CURLOPT_PORT An alternative port number to connect to. CURLOPT_HTTPHEADER An array of HTTP header fields to set. CURLOPT_FILE The file that the transfer should be written to. The default is STDOUT (the browser window). 11 / 13 The PHP Client URL (cURL) library
  • 12.
    The cURL libraryLicense Further information http://www.php.net/manual/en/book.curl.php 12 / 13 The PHP Client URL (cURL) library
  • 13.
    The cURL libraryLicense License This document is lincensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. http://creativecommons.org/licenses/by-nc-sa/3.0/ 13 / 13 The PHP Client URL (cURL) library