Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web Services Essentials. What is a web service? web service: software functionality that can be invoked through the internet using common protocols like.

Similar presentations


Presentation on theme: "Web Services Essentials. What is a web service? web service: software functionality that can be invoked through the internet using common protocols like."— Presentation transcript:

1 Web Services Essentials

2 What is a web service? web service: software functionality that can be invoked through the internet using common protocols like a remote function(s) you can call by contacting a program on a web server many web services accept parameters and produce results can be written in PHP and contacted by the browser in XHTML and/or Ajax code service's output is often not HTML but rather text, XML, JSON, or other content types

3 Content ("MIME") types

4 Setting content type with header by default, a PHP script's output is assumed to be HTML use the header function to specify non-HTML output must appear before any other output generated by the script

5 Example: Exponent web service Write a web service that accepts a base and exponent and outputs base raised to the exponent power. For example, the following query should output 81 :

6 Recall: HTTP GET vs. POST HTTP: the set of commands understood by a web server and sent from a browser GET : asks a server for a page or data. if the request has parameters, they are sent in the URL as a query string POST : submits data to a web server and retrieves the server's response. if the request has parameters, they are embedded in the request's HTTP packet, not the URL For submitting data, a POST request is more appropriate than a GET

7 GET or POST? Two different actions can be performed on the same URL with different methods. How?

8 Emitting partial-page HTML data

9 some web services do output HTML, but not a complete page the partial-page HTML is meant to be fetched by Ajax and injected into an existing page

10 Emitting XML data specify a content type of text/xml or application/xml print an XML prologue (the <?xml line) first important: no whitespace output can precede the prologue then print each line of XML data/tags as output some PHP libraries automatically generate XML for you from other data

11 Reporting errors how does a web service indicate an error to the client? error messages (print) are not ideal, because they could be confused for normal output web service should return an HTTP "error code" to the browser, possibly followed by output these are the codes you see in Firebug's console and in your Ajax request's.status property

12 Error Codes

13 Using headers for HTTP error codes

14 header can also be used to send back HTTP error codes – header("HTTP/1.1 403 Forbidden"); – header("HTTP/1.1 404 File Not Found"); – header("HTTP/1.1 500 Server Error");

15 How to send data to the client? There are many ways – XML – JSON

16 What is JSON JSON is a data interchange format Interactive Web 2.0 applications, no more use page replacement. Data transfer without refreshing a page. The most important aspects of data transfer are simplicity, extensibility, interoperability, openness and human readability Key idea in AJAX – Asynchronous Java Script and XML.

17 How does it work? JSON is a subset of Java Script. JSON can be parsed by a JavaScript parser. It can represent either complex or simple data as it has data types They are Strings, Number, Boolean, Objects and Arrays E.g. of Object: { "name": "Jack (\"Bee\") Nimble", "format": { "type": "rect", "width": 120, "interlace": false}}

18 Why is JSON better suited for AJAX? JSON is widely used in AJAX. The X in AJAX stands for XML. E.g. { "fullname": "Swati Kumar", "org": "Columbia", } Swati Kumar Columbia

19 How to handle JSON In PHP – Json_encode(associative array) – create JSON from associative array – Json_decode() – create associative array from JSON string In JavaScript – JSON.parse() – create a JavaScript object from JSON string – JSON. Stringify() – Create a string from JavaScript object

20 JSON example Go to code demonstration

21 Example Credit card validation Browser ProcessCard.php Card Company (Bank) Our application Outside Application Process.php cURL request JSON Response

22 Our Simulation (Setup) Wamp Server Linux Virtual Server Our ApplicationBank Application

23 Word on CURL You must install cURL before using it. Study how to ad curl to your WAMP installation.


Download ppt "Web Services Essentials. What is a web service? web service: software functionality that can be invoked through the internet using common protocols like."

Similar presentations


Ads by Google