Bài giảng Lập trình Web - Chapter 8: Services in PHP

Services in PHP  
Web Services  
• Web based applications  
• HTTP is the interface  
• Data is transmitted in XML or JSON  
• Generally intended for machine to machine  
communication  
• Required for mashups (generally)  
HTTP  
• GET  
– Requests data from a server  
• POST  
– Sends data to a server  
• PUT  
– Pushes a file onto a server  
• DELETE  
– Asks server to delete a resource  
• HEAD  
– Gets http header back from server, not whole resource  
HTTP Responses  
• 2xx = things are good  
– 200: OK, 201: created, 204: returning no content  
• 3xx = redirect  
– Head(‘location … causes 300 class HTTP response  
from your server  
HTTP Responses  
• 4xx = client error  
– 401: unauthorized, 403: forbidden, 404: not  
found, 411: length required  
• 5xx = server error  
– 500: error, 501: not implemented, 503: Service  
Unavailable  
Web Services…  
Service A  
HTTP  
XML  
HTTP  
Host  
web  
server  
Service B  
Service C  
Client  
HTML  
Database  
AJAX Web Services…  
Service A  
HTTP  
JSON/XML/TEXT  
Client  
Side JS  
engine  
Service B  
DOM  
Host  
web  
server  
Client  
HTML  
Service C  
Database  
An example  
• Load up this URL in your browser (add an  
address)  
• You could load this with simplexml_load_file()  
address here>>&output=xml&sensor=false  
Web Service Flavors  
• SOAP  
– XML wrapper for HTTP data transfer  
– Can define any method (beyond Get, Post &c)  
– Lots of overhead  
• REST  
– Lighter than SOAP  
– Only supports HTTP methods  
REST  
• Client gets a URL like  
example.com/products  
• Server sends back XML  
<products>  
<product id=‘1234’>A Chair</product>  
<product id=‘4958’>A Table</product>  
</products>  
REST  
• Client gets a URL like  
example.com/products/1234  
• Server sends back XML  
….  
<product id=‘1234’>  
<cost units=‘us dollars’>45.00</cost>  
<color>green</color>  
</product>  
REST  
• Imagine the whole data structure is in XML  
– (but really, we are just getting data out of any  
database)  
• Subfolders in the URL move in the complete  
pretend XML model like XPATH  
• Snippets of XML are returned  
• Examples:  
GET / POST Examples  
• Generate your own HTTP requests from PHP  
to other servers/services:  
• Easy to use Basic HTTP auth between services  
Listen for GET or POST  
• Apache/PHP Already does that!  
• Requests don’t have to come from web  
browsers  
• Get data via $_GET[‘varname’] and  
$_POST[‘varname’]  
• Output XML or JSON (or anything…but  
structure makes it easiest for the people using  
your web service)  
JSON  
Java Script Object Notation  
JSON is  
• A lightweight data interchange format  
• A replacement for XML  
• Human readable  
• Supports both hierarchical and unordered  
data  
• Supported by most programming languages  
• Supported by many modern web services  
– Flickr, Blogger, &c  
Declaring a JSON object  
var mydata = {};  
var mbject{}  
Name Value Pairs (unorderd)  
var js =  
{“runs” : “client”,  
“released” : 1995”,  
“type” : “scripting”};  
Adding Depth  
var lang = {  
js : {“runs” : “client”,  
“released” : 1995”,  
“type” : “scripting”},  
php: {“runs” : “server”,  
“released” : 1994”,  
“type” : “scripting”}  
};  
Arrays (ordered data)  
var employers =  
{
colleges : [“Sage”, “HVCC”, “RPI”]  
};  
Tải về để xem bản đầy đủ
pdf 27 trang myanh 10560 Free
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Lập trình Web - Chapter 8: Services in PHP", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pdfbai_giang_lap_trinh_web_chapter_8_services_in_php.pdf