Php webservices
By: Ilia Alshanetsky
The REST Way
Representational State Transfer (REST)
Concept coined by Roy Fielding
Uses the web or to be precise HTTP or HTTPS exclusive for transmitting web service request and response. Classic setup:
Input via GET/POST Output as XML document
XML 2
REST Request
http://site.com/forum/rss.php?latest=1
That’sall folks Want to make a remote request? No problem! $url = “…”; $response = file_get_contents($url);
XML 3
REST Response
First Post!!! http://fudforum.org/index.php/m/1 1st message in the forum. Re: First Post!!! http://fudforum.org/index.php/m/1 Almost like Slashdot.
XML
4
Parsing XML Response
To parse the returned XML we turn to any number of extensions found in PHP.
XML extension
Basic XML parser based on SAX methodology found in all PHP versions. Arguably the simplest XML parser to use. Maximum flexibility for both parsing and creating XML Pull parser, that combines ease of use with high performance.
XML 5
SimpleXML
DOM
XMLReader
XML Parsing Methodologies
SAX
(Simple API for XML)
DOM
(Document ObjectModel)
An event based approach where by each action, such “found new tag” needs to be handled. The triggerable events include:
open tag close tag tag’s data
Loads the entire document into memory, creating a “tree” representing the XML data. The tree can then be traversed in multitude of ways.
XML
6
Simple API for XML
Uses little memory. Allows work to startsimmediately. Works well with remote XML data source. Same parsing API in PHP 4 and 5
All those handler calls are slow. Only Sequential data access. Can’t easily retrieve a particular document segment. Requires lot’s of PHP code. Read-only.
7
XML
Document Object Model
Very fast for small documents. Access anything anytime. Simpler PHP interface. UnderlyingXML parsing library, libXML2 is better suited for DOM.
“All your memory are belong to DOM”. Data only usable after the complete document is retrieved parsed. You’ll need PHP 5+.
XML
8
PHP’s XML Parsers
SAX
(Simple API for XML)
DOM
(Document Object Model)
XML XMLReader (PECL) XMLWriter (PECL)
SimpleXML DOM (PHP5) DOMXML (PHP 4) XSLT (domengine) SOAP (dom engine) XML-RPC (dom engine)
XML
9
Biggest Gripe About Sax
One of the biggest complaints about SAX is that it PHP requires the developer to write a lot of code and be fully aware of the XML being parsed.
XML
10
It can’t be that bad, can it?
class xmlParser { private $x, $file, $cur_tag, $cur_id; public $data_store = array(), $n_entries = 0; function__construct($xml_file) { $this->file = $xml_file; $this->x = xml_parser_create(); xml_set_object($this->x, $this); xml_set_element_handler($this->x, "startTag", "endTag"); xml_set_character_data_handler($this->x, ‘tagContent'); } function parse() { $fp = fopen($this->file, "r"); while (!feof($fp)) xml_parse($this->x, fread($fp, 1024), feof($fp)); fclose($fp); } function __destruct() {xml_parser_free($this->x); }
XML 11
Callbacks
function startTag($parser, $tag_name, $attribute) { if ($tag_name == 'ITEM') { $this->cur_id = (int)$attribute['ID']; $this->data_store[$this->cur_id] = array(); } else if ($tag_name != 'FORUM') $this->data_store[$this->cur_id][$tag_name] = ''; $this->cur_tag = $tag_name; } function endTag($parser, $tag_name) { if ($tag_name == 'ITEM') ++$this->n_entries; }function tagContent($parser, $data) { if (in_array($this->cur_tag, array ('TITLE','LINK','DESCRIPTION'))) $this->data_store[$this->cur_id][$this->cur_tag] .= $data; }
XML 12
Case Sensitivity
One of things XML shares with HTML is the inherent case-insensitivity of the tags. The XML extensions automatically “solves” this problem for you by case-folding all tags to uppercase.
To...
Regístrate para leer el documento completo.