Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Rob Allen:
Throw an exception when simplexml_load_string fails
Sep 09, 2014 @ 14:27:13

In a quick post to his site Rob Allen shares a class that he's created to handle and throw an exception any time that the load from a SimpleXML parsing fails.

I keep having to look up how to stop the warning that are emitted when simplexml_load_string & simplexml_load_file fail, so this time I've written the world's simplest little class to take care of it for me from now on.

His "Xml" class wraps around the SimpleXML functionality and checks to see if the resulting object is false. If it is, it uses some internal error handling to fetch the error message result and throws it as a "RuntimeException". This error string comes from a "getXMLErrorString" function that uses the libxml_get_errors function to get the resulting error list.

tagged: simplexml load string file fail exception error handling

Link: http://akrabat.com/php/throw-an-exception-when-simplexml_load_string-fails/

PHPMaster.com:
Parsing XML With SimpleXML
Feb 12, 2013 @ 18:48:34

On PHPMaster.com today there's a new tutorial introducing you to SimpleXML, a handy bit of functionality included with the base PHP install to make working with XML (well, reading it) much simpler.

Parsing XML essentially means navigating through an XML document and returning the relevant data. An increasing number of web services return data in JSON format, but a large number still return XML, so you need to master parsing XML if you really want to consume the full breadth of APIs available. Using PHP’s SimpleXML extension that was introduced back in PHP 5.0, working with XML is very easy to do. In this article I’ll show you how.

He starts with some basic usage of the SimpleXML parsing, giving an example XML to parse, the resulting object and how to access the data inside it. There's also a bit about dealing with namespaces in the XML you're parsing and a more practical example - parsing the output of a YouTube feed to get links to various videos.

tagged: parse xml simplexml introduction tutorial

Link:

PHPMaster.com:
Bending XML to Your Will
Jan 26, 2012 @ 17:04:44

PHPMaster.com has a new tutorial posted today about "bending XML to your will" - working with XML data using the XML Parser and SimpleXML functionality already included with PHP.

If you've ever worked with the Twitter or Facebook APIs, looked at RSS feeds from a website, or made use of some type of RPC calls, you've undoubtedly experienced working with XML. [...] Knowing how to process XML data is a crucial programming skill today, and thankfully, PHP offers multiple ways to read, filter, and even generate XML. In this article I'll explain what exactly XML is, in case you haven't had any experience with it yet, and then dive into a few ways you can use PHP to bend XML to your will.

He introduces the concepts and syntax behind XML first for those not accustomed and quickly moves into the tools to parse it. First he looks at the XML Parser, an event-based parser, and SimpleXML, an easy way to access the contents of a well-formed XML document. Snippets of code are provided for each to show you how to put them into practice.

tagged: xml tutorial parse eventbased simplexml introduction

Link:

SitePoint.com:
How to Create an XML to JSON Proxy Server in PHP
Oct 19, 2011 @ 18:07:08

On SitePoint.com today there's a new post from Craig Buckler showing you how to create a simple XML to JSON proxy server in PHP with a SimpleXML object at its heart.

Unless you're new to this web development lark, you'll know the 'X' in 'AJAX' stands for XML - eXtensible Markup Language. But you’re probably not using XML. If you are, you'd probably prefer not to. All the cool kids are using JSON or JSON-P: it has a smaller payload, is easier to use and faster to process. [...] Fortunately, there are a couple of solutions which allow you to retain the benefits of XML data interchange but provide the ease of JSON in JavaScript. In this article, we’re going to create an XML to JSON proxy server in PHP.

Of course, this will only work with well-formatted XML documents, but it's a quick little hack that pulls in the XML data with a curl request and parses it via SimpleXML and uses json_encode to push it back out as JSON.

tagged: xml translate json proxy server tutorial simplexml

Link:

Enrico Zimuel's Blog:
XML to JSON in PHP: an odyssey
May 03, 2011 @ 17:05:09

Enrico Zimuel as a new post to his blog today talking about his exploration of a Zend Framework bug related to the JSON handling of the Zend_Json component. The issue was related to the conversion of XML to JSON but it only happened in specific instances.

As you can see the bar value, of the a element [in the example code], is not represented in JSON. This issue comes also with other XML documents, and in general when an XML node has a single character data child, any attributes are lost.

In tracking down the issue, he followed a lead through a possible SimpleXML issue and, with some further testing, found a simple, non-Zend Framework script to behave the same way. It dropped the "bar" value of the example XML from the object's return value. To get around this bug, he created a new conversion algorithm that redefined the _processXML method in Zend_Json to work with the Zend_Json_Expr functionality to parse things more correctly. If you can't wait for the next release to get it into your codebase, you can grab the code from his post.

tagged: zendframework xml json simplexml custom

Link:

iFadey.com:
Get Flickr Images Using SimpleXML
Dec 16, 2010 @ 18:04:20

In this new post to the iFadey blog, there's a quick tutorial on using SimpleXML to fetch and parse the image feeds from Flickr.

In this article you will learn two main things: how to parse XML using SimpleXML and how to get Flickr images from RSS feed and display them on your web page.

He offers both a demo of it in action and a download of the source for those that want to jump right in. The tutorial walks you through fetching the XML feed for a Flickr gallery, loading it into a SimpleXML object and creating a reusable class to pull out the photo titles, image details and profile link.

tagged: flickr simplexml image tutorial xml

Link:

Lorna Mitchell's Blog:
Google Analytics Accounts API
Nov 23, 2010 @ 16:56:35

In this new post to her blog Lorna Mitchell looks at some of the work she's done to extract information out of the Google Analytics accounts API via an OAuth interface and parsed via SimpleXML.

I'm using pecl_oauth to authenticate users against their google accounts (see my post about using OAuth from PHP), but even after I have a valid google user, working out which analytics accounts they have access to and how to refer to them is a puzzle in itself, so I thought I'd share what I learned.

In her example she shows how to fetch the permissions for a user (which analytics they can access) via a call to the "/feeds/datasources/ga/accounts" URL. This returns some XML easily parsed by SimpleXML to grab the accounts' names and IDs. This is used to fetch the profiles and gather the "magic tableId" for use with the Export API to pull the actual data.

tagged: google analytics account api oauth tutorial simplexml

Link:

CodingThis.com Blog:
Quick XML-to-HTML Templating in PHP Using SimpleXML
Nov 11, 2010 @ 19:03:04

New on the CodingThis.com blog there's a post about templating using SimpleXML to parse a basic XML file and push the data into a template.

Since the API method interface is relatively static, I didn’t need to generate it on the fly, so I created a PHP script. I could have used an XSL Transform (XSLT), but instead I decided to use pure PHP. (I’d rather use PHP and realize I have too much power than use XSLT, and realize I don’t have enough. Plus, I love PHP. There’s no shame in sticking with what you know.) [...] The best package I found for my purposes is SimpleXML, which mimics the tree structure of an XML file as a series of embedded objects.

He gives an example of some simple XML returned from his REST API, how parsed it and a foreach loop to go through the methods and output the HTML with the correct values.

tagged: template simplexml tutorial rest api

Link:

Lorna Mitchell's Blog:
Fetching Namespaced XML Elements With SimpleXML
Nov 04, 2010 @ 15:48:14

Lorna Mitchell has a new post to her blog with a quick example of how to get namespaced values with SimpleXML out of a properly formatted XML file.

Recently I was working with some google APIs and needed to retrieve some namespaced elements from the result set. This confused me more than I expected it to so here's my code for the next time I need it (and if you use it too, then great!) I was reading from their analytics data feed API, this returns a few key fields and then multiple tags, each with namespaced children.

The trick is to grab the current document's namespaces (via getNamespaces), finding the key for the ones you want and grabbing them as children.

tagged: simplexml namespace element xml tutorial

Link:

Komunitasweb.com:
Showing the weather with PHP and Google Weather API
Sep 11, 2009 @ 14:47:21

On the Komunitasweb.com blog there's a recent post walking you through the steps to add the Google weather content to your site, complete with icons.

If you need to show weather in your website, you can use weather widget such as weatherbug. It’s nice and simple, but maybe you need something more integrated with your website. So, take a look at Google Weather API.

They use a SimpleXML object to pull in the XML data from Google's backend (as called via a URL) and loop through the resulting objects to output temperature, current conditions and several days of the forecast.

tagged: weather tutorial google simplexml api

Link:


Trending Topics: