[title] => Mozart's Requiem
[time] => 1330196400
In this example, weve simply taken the string output by json_encode() and
translated it back into a PHP array. Since we do want an associative array, rather
than an object, we pass true as the second parameter to json_decode(). Without
this, wed have an array containing three stdClass objects, each with properties
called title and time.
As is clear from these examples, JSON is simple to work with in PHP, and as such
it is a popular choice for all kinds of web services.
Working with XML
Having seen the example with JSON, lets look at another commonly used data
format, XML. XML stands for eXtensible Markup Language; its the standard way
of representing machine-readable data on many platforms.
XML is a more verbose format than JSON. It contains more data-type information
and different systems will use different tags and attributes to describe information
in great detail. XML can be awkward for humans to read, but its ideal for machines
as it is such a prescriptive format. As a result, its a good choice for use when integ-
rating two systems exchanging important data unsupervised.
In PHP, there is more than one way of working with XML; the main players here
are the DOM extension or the SimpleXML extension. Their functionality overlaps
greatly; however, in a nutshell, DOM could be described as more powerful and
complex, while SimpleXML is more, well, simple! You can switch between formats
with a single function call, so its trivial to begin with one and flip to using the
other for a particular operation. Since were working with basic examples, the code
shown here will use the SimpleXML extension.
Lets start with an example along the same lines as the JSON one above:
PHP Master: Write Cutting-edge Code78
$simplexml = new SimpleXMLElement(
'<?xml version="1.0"?><concerts />');
$concert1 = $simplexml->addChild('concert');
$concert1->addChild("title", "The Magic Flute");
$concert1->addChild("time", 1329636600);
$concert2 = $simplexml->addChild('concert');
$concert2->addChild("title", "Vivaldi Four Seasons");
$concert2->addChild("time", 1329291000);
$concert3 = $simplexml->addChild('concert');
$concert3->addChild("title", "Mozart's Requiem");
$concert3->addChild("time", 1330196400);
echo $simplexml->asXML();
/* output:
<concerts><concert><title>The Magic Flute</title><time>1329636600
</time></concert><concert><title>Vivaldi Four Seasons</title>
<time>1329291000</time></concert><concert><title>Mozart's Requiem
Lets start from the top of the file and work through this code example. First of all,
we create a SimpleXMLElement, which expects a well-formed XML string to pass to
the constructor. This is great if we want to read and work with some existing XML
(and will be really handy when we parse incoming requests with XML data in them),
but feels a little clunky when were creating the empty element.
Then we move on and start adding elements. In XML, we cant have enumerated
items; everything needs to be inside a named tag, so each concert item is inside a
tag named concert. When we add a child, we can also assign it to a variable, and
this allows us to continue to operate on it. In this case, we want to add more children
to it, so we capture it in $concert1, and then add the title and time tags as chil-
We repeat for the other concerts (youd probably use a looping construct on data
pulled from elsewhere in a real application), and then output the XML using the
SimpleXMLElement::asXML() method. This method literally outputs the XML that
this object represents.
When we come to read XML, this is fairly trivial:
chapter_03/xml_load_string.php (excerpt)
$xml = '<concerts><concert><title>The Magic Flute</title><time>
1329636600</time></concert><concert><title>Vivaldi Four Seasons
Mozart\'s Requiem</title><time>1330196400</time></concert>
$concert_list = simplexml_load_string($xml);
/* output:
SimpleXMLElement Object
[concert] => Array
[0] => SimpleXMLElement Object
[title] => The Magic Flute
[time] => 1329636600
[1] => SimpleXMLElement Object
[title] => Vivaldi Four Seasons
[time] => 1329291000
[2] => SimpleXMLElement Object
[title] => Mozart's Requiem
[time] => 1330196400
PHP Master: Write Cutting-edge Code80

Get PHP Master now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.