SlideShare uma empresa Scribd logo
1 de 13
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados

Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programmingelliando dias
 
Introduction to tcpdump
Introduction to tcpdumpIntroduction to tcpdump
Introduction to tcpdumpLev Walkin
 
บทที่ 2 โพรโตคอล (protocol)
บทที่  2 โพรโตคอล (protocol)บทที่  2 โพรโตคอล (protocol)
บทที่ 2 โพรโตคอล (protocol)chrisman77
 
2. reverse primarydns using bind for ptr and cname record ipv4
2. reverse primarydns using bind for ptr and cname record ipv42. reverse primarydns using bind for ptr and cname record ipv4
2. reverse primarydns using bind for ptr and cname record ipv4Piyush Kumar
 
Maksim Melnikau aka “max_posedon” - Telepathy protocol example
Maksim Melnikau aka “max_posedon” - Telepathy protocol exampleMaksim Melnikau aka “max_posedon” - Telepathy protocol example
Maksim Melnikau aka “max_posedon” - Telepathy protocol exampleMinsk Linux User Group
 
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونیMohammad Reza Kamalifard
 
Network programming Using Python
Network programming Using PythonNetwork programming Using Python
Network programming Using PythonKarim Sonbol
 
Iss letcure 7_8
Iss letcure 7_8Iss letcure 7_8
Iss letcure 7_8Ali Habeeb
 
Nodejs 프로그래밍 ch.3
Nodejs 프로그래밍 ch.3Nodejs 프로그래밍 ch.3
Nodejs 프로그래밍 ch.3HyeonSeok Choi
 
Networking & Socket Programming In Java
Networking & Socket Programming In JavaNetworking & Socket Programming In Java
Networking & Socket Programming In JavaAnkur Agrawal
 

Mais procurados (19)

Linux comands for Hadoop
Linux comands for HadoopLinux comands for Hadoop
Linux comands for Hadoop
 
socket programming
socket programming socket programming
socket programming
 
Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programming
 
skipfish
skipfishskipfish
skipfish
 
Introduction to tcpdump
Introduction to tcpdumpIntroduction to tcpdump
Introduction to tcpdump
 
บทที่ 2 โพรโตคอล (protocol)
บทที่  2 โพรโตคอล (protocol)บทที่  2 โพรโตคอล (protocol)
บทที่ 2 โพรโตคอล (protocol)
 
2. reverse primarydns using bind for ptr and cname record ipv4
2. reverse primarydns using bind for ptr and cname record ipv42. reverse primarydns using bind for ptr and cname record ipv4
2. reverse primarydns using bind for ptr and cname record ipv4
 
Maksim Melnikau aka “max_posedon” - Telepathy protocol example
Maksim Melnikau aka “max_posedon” - Telepathy protocol exampleMaksim Melnikau aka “max_posedon” - Telepathy protocol example
Maksim Melnikau aka “max_posedon” - Telepathy protocol example
 
Basics of sockets
Basics of socketsBasics of sockets
Basics of sockets
 
Java sockets
Java socketsJava sockets
Java sockets
 
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
 
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
 
Network programming Using Python
Network programming Using PythonNetwork programming Using Python
Network programming Using Python
 
Iss letcure 7_8
Iss letcure 7_8Iss letcure 7_8
Iss letcure 7_8
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Nodejs 프로그래밍 ch.3
Nodejs 프로그래밍 ch.3Nodejs 프로그래밍 ch.3
Nodejs 프로그래밍 ch.3
 
libpcap
libpcaplibpcap
libpcap
 
Network Sockets
Network SocketsNetwork Sockets
Network Sockets
 
Networking & Socket Programming In Java
Networking & Socket Programming In JavaNetworking & Socket Programming In Java
Networking & Socket Programming In Java
 

Semelhante a Php client libray

Performance #4 network
Performance #4  networkPerformance #4  network
Performance #4 networkVitali Pekelis
 
Go 1.8 'new' networking features
Go 1.8 'new' networking featuresGo 1.8 'new' networking features
Go 1.8 'new' networking featuresstrikr .
 
Consuming RESTful Web services in PHP
Consuming RESTful Web services in PHPConsuming RESTful Web services in PHP
Consuming RESTful Web services in PHPZoran Jeremic
 
Consuming RESTful services in PHP
Consuming RESTful services in PHPConsuming RESTful services in PHP
Consuming RESTful services in PHPZoran Jeremic
 
Lecture8_AdvancedPHP(Continue)-APICalls_SPring2023.pdf
Lecture8_AdvancedPHP(Continue)-APICalls_SPring2023.pdfLecture8_AdvancedPHP(Continue)-APICalls_SPring2023.pdf
Lecture8_AdvancedPHP(Continue)-APICalls_SPring2023.pdfShaimaaMohamedGalal
 
Http request&response by Vignesh 15 MAR 2014
Http request&response by Vignesh 15 MAR 2014Http request&response by Vignesh 15 MAR 2014
Http request&response by Vignesh 15 MAR 2014Navaneethan Naveen
 
Web Server and how we can design app in C#
Web Server and how we can design app  in C#Web Server and how we can design app  in C#
Web Server and how we can design app in C#caohansnnuedu
 
Hands on with CoAP and Californium
Hands on with CoAP and CaliforniumHands on with CoAP and Californium
Hands on with CoAP and CaliforniumJulien Vermillard
 
What I learned about APIs in my first year at Google
What I learned about APIs in my first year at GoogleWhat I learned about APIs in my first year at Google
What I learned about APIs in my first year at GoogleTim Burks
 
Introduction to gRPC - Mete Atamel - Codemotion Rome 2017
Introduction to gRPC - Mete Atamel - Codemotion Rome 2017Introduction to gRPC - Mete Atamel - Codemotion Rome 2017
Introduction to gRPC - Mete Atamel - Codemotion Rome 2017Codemotion
 
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...Codemotion
 
Android Performance #4: Network
Android Performance #4: NetworkAndroid Performance #4: Network
Android Performance #4: NetworkYonatan Levin
 
Flex with AMFPHP
Flex with AMFPHPFlex with AMFPHP
Flex with AMFPHPtusay
 
Flex with AMFPHP
Flex with AMFPHPFlex with AMFPHP
Flex with AMFPHPtusay
 

Semelhante a Php client libray (20)

Fidl analysis
Fidl analysisFidl analysis
Fidl analysis
 
Http request&response
Http request&responseHttp request&response
Http request&response
 
Performance #4 network
Performance #4  networkPerformance #4  network
Performance #4 network
 
Go 1.8 'new' networking features
Go 1.8 'new' networking featuresGo 1.8 'new' networking features
Go 1.8 'new' networking features
 
Consuming RESTful Web services in PHP
Consuming RESTful Web services in PHPConsuming RESTful Web services in PHP
Consuming RESTful Web services in PHP
 
Consuming RESTful services in PHP
Consuming RESTful services in PHPConsuming RESTful services in PHP
Consuming RESTful services in PHP
 
Lecture8_AdvancedPHP(Continue)-APICalls_SPring2023.pdf
Lecture8_AdvancedPHP(Continue)-APICalls_SPring2023.pdfLecture8_AdvancedPHP(Continue)-APICalls_SPring2023.pdf
Lecture8_AdvancedPHP(Continue)-APICalls_SPring2023.pdf
 
PPT
PPTPPT
PPT
 
Http request&response by Vignesh 15 MAR 2014
Http request&response by Vignesh 15 MAR 2014Http request&response by Vignesh 15 MAR 2014
Http request&response by Vignesh 15 MAR 2014
 
TensorFlow XLA RPC
TensorFlow XLA RPCTensorFlow XLA RPC
TensorFlow XLA RPC
 
Web Server and how we can design app in C#
Web Server and how we can design app  in C#Web Server and how we can design app  in C#
Web Server and how we can design app in C#
 
Http VS. Https
Http VS. HttpsHttp VS. Https
Http VS. Https
 
Hands on with CoAP and Californium
Hands on with CoAP and CaliforniumHands on with CoAP and Californium
Hands on with CoAP and Californium
 
What I learned about APIs in my first year at Google
What I learned about APIs in my first year at GoogleWhat I learned about APIs in my first year at Google
What I learned about APIs in my first year at Google
 
Introduction to gRPC - Mete Atamel - Codemotion Rome 2017
Introduction to gRPC - Mete Atamel - Codemotion Rome 2017Introduction to gRPC - Mete Atamel - Codemotion Rome 2017
Introduction to gRPC - Mete Atamel - Codemotion Rome 2017
 
Copy of cgi
Copy of cgiCopy of cgi
Copy of cgi
 
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...
 
Android Performance #4: Network
Android Performance #4: NetworkAndroid Performance #4: Network
Android Performance #4: Network
 
Flex with AMFPHP
Flex with AMFPHPFlex with AMFPHP
Flex with AMFPHP
 
Flex with AMFPHP
Flex with AMFPHPFlex with AMFPHP
Flex with AMFPHP
 

Mais de maamir farooq (20)

Ooad lab1
Ooad lab1Ooad lab1
Ooad lab1
 
Lesson 03
Lesson 03Lesson 03
Lesson 03
 
Lesson 02
Lesson 02Lesson 02
Lesson 02
 
Swiftmailer
SwiftmailerSwiftmailer
Swiftmailer
 
Lect15
Lect15Lect15
Lect15
 
Lec 7
Lec 7Lec 7
Lec 7
 
Lec 6
Lec 6Lec 6
Lec 6
 
Lec 5
Lec 5Lec 5
Lec 5
 
J query 1.7 cheat sheet
J query 1.7 cheat sheetJ query 1.7 cheat sheet
J query 1.7 cheat sheet
 
Assignment
AssignmentAssignment
Assignment
 
Java script summary
Java script summaryJava script summary
Java script summary
 
Lec 3
Lec 3Lec 3
Lec 3
 
Lec 2
Lec 2Lec 2
Lec 2
 
Lec 1
Lec 1Lec 1
Lec 1
 
Css summary
Css summaryCss summary
Css summary
 
Manual of image processing lab
Manual of image processing labManual of image processing lab
Manual of image processing lab
 
Session management
Session managementSession management
Session management
 
Data management
Data managementData management
Data management
 
Content provider
Content providerContent provider
Content provider
 
Android sq lite database tutorial
Android sq lite database tutorialAndroid sq lite database tutorial
Android sq lite database tutorial
 

Último

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 

Último (20)

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 

Php client libray

  • 1. 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
  • 2. The cURL library License Outline 1 The cURL library 2 / 13 The PHP Client URL (cURL) library
  • 3. 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
  • 4. 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
  • 5. 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
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. 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
  • 10. 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
  • 11. 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
  • 12. The cURL library License Further information http://www.php.net/manual/en/book.curl.php 12 / 13 The PHP Client URL (cURL) library
  • 13. 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