PHP Tip of the Week
I HATE XML!
I really can't adequately express my distaste for XML. I've never enjoyed working with it. I understand the need for it and why we have it but I absolutely hate working with it. Recently, I had to use it to process the feed from my podcast and I was reminded again how much I hate XML.
In my research, I found this one little 3- line tip that makes working with XML so much more manageable. The PHP manual has user contributed notes. On the SimpleXML page, the first user contributed note shows how to convert XML to JSON with three lines of code.
$xml = simplexml_load_string($feed);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
SALVATION! I never have to work with XML again...well, almost. First, this code works in a lot of cases but with a few caveats. In the case of an RSS feed, it has some problems.
- First Problem
I like dealing with JSON objects, not arrays, so on line three in the snippet above, I left out the second parameter to json_decode. This gives me objects to work with. However, because of how strange XML is, this means that I now have objects with parameters names things like '@attributes.' Since '@' is not a legal character in parameter names, I can't access large parts of the data that has been converter. So yes, you have to use arrays.
- Second Problem
A lot of XML documents, RSS feeds included, wrap some segments in CDATA tags. The code above won't preserve CDATA segments and your json array won't include anything that is wrapped in CDATA tags. Thankfully, Stack Overflow has the solution. When you create the XML, you need to pass in the 'LIBXML_NOCDATA' parameter.
$xml = simplexml_load_string($xml_string,'SimpleXMLElement',LIBXML_ NOCDATA);
Now any content inside a CDATA tag will be preserved but the CDATA tags will be stripped out.
The solution is not perfect. There are still oddities in XML that won't be preserved. However, in many cases, this will give you a quick and easy solution to dealing with XML. And if you hate XML as much as I do, that's a good thing.
Effective Redis for PHP Developers
February 16, 2017 | 20:00 CST
Redis is a key-value store that has carved out a role for itself as a data structure server. If you’re using it only for caching simple values, you’re tapping a mere fraction of its power. This presentation provides an introduction to Redis and the Predis client library for it as well as related data structures that Redis supports and when it’s best to use them. No computer science background is necessary; data structures are presented in understandable terms with practical examples.
PHP Authentication: Lessons Learned
February 16, 2017 | 20:00 CET
Just about every website out there has some sort of authentication. We sign into accounts everywhere, from your social media profile all the way to your bank account. Brian will share what he has learned through the years about authentication in PHP. Giving real life examples of the ugly authentication that he started with, which you may have as well, through what is now the best practices of authenticating your web applications.