0% found this document useful (0 votes)
2 views4 pages

Data Formats & Types

The document is a comprehensive PHP study guide covering various data formats and types, including XML, JSON, and web services like REST and SOAP. It provides detailed explanations of parsing XML using DOM and SimpleXML, as well as the use of XPath for searching XML documents. Additionally, it discusses the implementation of web services and the use of DateTime in PHP.

Uploaded by

coolakki20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Data Formats & Types

The document is a comprehensive PHP study guide covering various data formats and types, including XML, JSON, and web services like REST and SOAP. It provides detailed explanations of parsing XML using DOM and SimpleXML, as well as the use of XPath for searching XML documents. Additionally, it discusses the implementation of web services and the use of DateTime in PHP.

Uploaded by

coolakki20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Data Formats & Types

PHP Study Guide


PHP Basics
Functions
Strings & Patterns
Arrays
I/O
Security
Databases
Object Oriented Programming
Data Formats & Types
Web Features
Design and Theory
Data Formats & Types
XML
Parsing XML
SimpleXml
DOM
XPath
XPath Searches
Web Services
REST
SOAP
DateTime
JSON & AJAX
XML
Well Formed
Single root level tag
Tags must be opened and closed properly
Entities must be well formed
Valid
Well formed
Contain a DTD (Document Type Definition)
Follow that DTD
Parsing XML
DOM
Document Object Model
Loads entire document into ram, then creates an internal tree representation
http://php.net/dom
SimpleXML
All objects are instances of the SimpleXMLElement class
Uses DOM method
Very easy to use
Memory Intensive
http://php.net/simplexml
SimpleXml
$xml = simplexml_load_string($library);
object simplexml_load_file ( string $filename [, string $class_name [, int $options
[, string $ns [, bool $is_prefix]]]] )
SimpleXMLElement::xpath()
Adding elements
SimpleXMLElement::addChild()
SimpleXMLElement::addAttribute()
asXML()
$library->book[0] = NULL; - to remove child elements
children() - return iterator for subnodes
attributes()
Namespaces
SimpleXMLElement::getDocNamespaces()
SimpleXMLElement::getNamespaces()
DOM
http://php-guide.evercodelab.com/pages/data-formats-and-types.html
1/310/06/2017
Data Formats & Types
1 <?php
2
3
4 $dom = new DomDocument();
$dom->load("library.xml");
$dom->loadXML($xml);
5
6
7 DomDocument::loadHtmlFile(); // and DomDocument::loadHTML()
DomDocument::save(); // (to a file)
8
9
10
DomDocument::saveXML(); // (to a string)
DomDocument::saveHTML(); // (also to a string, but saves an HTML document instead
of an XML file)
DomDocument:saveHTMLFile(); // (to a file in HTML format).
DomNode
DomDocument::createElement()
DomDocument::createElementNS()
DomDocument::createTextNode()
DomNode::appendChild()
DomNode::insertBefore()
DomNode::cloneNode()
DomNode::setAttributeNS()
Removing
DomNode::removeAttribute()
DomNode::removeChild()
DomCharacterData::deleteData()
Import
dom_import_simplexml($sxml)
simplexml_import_dom($dom)
XPath
W3C Standard supported by many languages
Combines the mojo of Regex with some SQL like results
$xpath = new DomXPath($dom);
A call to DomXpath::query() will return a DomNodeList object;
http://www.w3schools.com/xpath/xpath_syntax.asp
XPath Searches
xpath("item") - will return an array of item objects
xpath("/bookshelf") - will return all children of the forum node
xpath("//book") - will return an array of title values
xpath(".") - will return the current node, <bookshelf>
xpath("..") - will return an empty array, root node <bookshelf> does not have a
parent element.
Web Services
Generic umbrella that describes how disparate systems can integrate with each other
over the web
Most major sites offer some form of web services:
Amazon
FedEx
eBay
PayPal
del.icio.us
Someone else has data you need
Easier than scraping pages (also more reliable)
Automate processes
Provide additional information to your clients
REST
Representational State Transfer
Requests look like filled out forms, can use either GET or POST
Response is an XML/JSON document
Request and Response is defined by the service provider
Services that use the REST architecture are referred to as RESTful services; those
who use or provide RESTful services are sometimes humorously referred to as
RESTafarians.
http://library.example.com/api.php?devkey=123&action=search&type=book&keyword=style
SOAP
Simple Object Access Protocol (Doesn’t actually stand for anything anymore)
Requests are sent using POST
Requests are encapsulated within a SOAP Envelope
Responses are XML documents with similar Envelope & Body sections
WSDL Documents
SOAP web services are described by WSDL files
They are designed for automated (not human) consumption
Describes the methods offered by the web service, the parameters required, and the
return type, as well as all complex types required by the service
http://php-guide.evercodelab.com/pages/data-formats-and-types.html
2/310/06/2017
Data Formats & Types
PHP5 has built in SOAP support
The functions (generally) take a WSDL file as input, and create an object that
mimics the services of the webservice:
1 <?php
2 $client = new
SoapClient("http://soap.amazon.com/schemas2/AmazonWebServices.wsdl");
1
2 <?php
$result = $client->KeywordSearchRequest($params);
API call:
Debugging
1 <?php
2
3 $client = new SoapClient('http://api.google.com/GoogleSearch.wsdl', array('trace'
=> 1));
$client->__getLastRequestHeaders();
4 $client->__getLastRequest();
PHP5 SOAP Server
doesn’t include creation of WSDL files (NuSOAP does, Zend Studio does)
You can still provide a service either by using an external WSDL file, or merely
defining the access methods
1 <?php
2
3 $options = array('uri' => 'http://example.org/soap/server/');
$server = new SoapServer(NULL, $options);
4 $server->setClass('MySoapServer');
5 $server->handle();
DateTime
http://php.net/manual/en/class.datetime.php
http://php.net/manual/en/book.datetime.php
JSON & AJAX
http://php.net/manual/en/book.json.php
0 Comments
 Recommend
1

PHP study guide
Login
Sort by Best
⤤ Share
Start the discussion…
Be the first to comment.
✉ Subscribe d Add Disqus to your siteAdd DisqusAdd
🔒 Privacy
http://php-guide.evercodelab.com/pages/data-formats-and-types.html
3/3

You might also like