When we originally designed the BeerMap API we decided to provide the data in a plist format, along with json and XML.
The plist format isn’t widely used for APIs, however iOS provides efficient inbuilt parsing of this file type and we also found this to be much easier to implement in terms of code complexity.
For example, you can just do this:
NSDictionary* dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@"http://www.example.com/data.plist"]];
For the past two years we’ve used the CFPropertyList library which in my opinion is the best and most functional library for reading and writing plist files in PHP.
If you find issues, please post on Github, or even better, fork and create a pull request!
However, in BeerMap we just need to create and output plists as fast as we can without any need to read or represent them in an object format. Looking at the xdebug output for the BeerMap API it became evident that the plist creation was by far the biggest bottleneck, mostly because of the XML object representation.
So, in summary I’ve created a quick class which just generates an XML string from an associative array instead of using XML objects. From my testing using ab, I was getting almost twice the number of request per second.
You can find the code at Github – https://github.com/paulbain/Bain-PHP-Library where I’ve started a library in the Zend Framework layout.
Here’s an example of how to use it:
require_once 'Plist.php'; //Get some data in an associative array $data = array( 'reviews'=>; array( array('title'=>;'My Review','comment'=>;'It\'s awesome','rating'=>;5) ) ); // Specifiy types for any non-string named keys $types = array( 'rating'=>;'real' ); //Create the Plist $plist = new Bain_Plist($data,null,$types); //Output the plist! (Maybe you'll need an XML header) echo $plist;
If you’re using the Zend Framework auto loader, just place the library in your path and register the namespace:
$autoloader = Zend_Loader_Autoloader::getInstance(); $autoloader->;registerNamespace('Bain_');
If you use this in a project leave a comment!