Check for Valid XML with PHP

Simple function to check if XML is valid. It just loads the XML into DOMDocument and checks for errors.

/**
*  Takes XML string and returns a boolean result where valid XML returns true
*/
function is_valid_xml ( $xml ) {
    libxml_use_internal_errors( true );
    
    $doc = new DOMDocument('1.0', 'utf-8');
    
    $doc->loadXML( $xml );
    
    $errors = libxml_get_errors();
    
    return empty( $errors );
}

Export data to Google spreadsheet with OAuth

I was working on exporting CSV data to a Google spreadsheet recently. The first thing I had to do was implement a way to allow a user to authenticate themselves with Google from our site. There are a few ways of doing this.

I choose OAuth as I thought it would be simplest. I found it was pretty straightforward but there were gaps in Google’s documentation that made the process a tad frustrating.

Here’s a brief outline…
Continue reading

Create a smart, cross browser floating menu using Javascript

I recently wrote a Javascript library to show a floating menu that works cross browser and displays the menu based on a set of co-ordinates that you pass it.

The floating menu is just an absolute div that is hidden from view.

The library is smart in the sense that it determines where best to display the menu. It works out where you have scrolled to on the page and figures out if there is space at the top or bottom and to the left or right of where ever your target co-ordinates are.

This is useful where you want to display a menu, or some div, but you don’t know (or care) where the menu is going to be needed on the page.
Continue reading

JavaScript Tricks; URLEncode, Foreach and Remove last character

I have come across some neat (if not mind blowing) tricks with JavaScript recently.

Firstly, I needed JavaScript code that copied PHP’s urlencode().
This function returns an encoded string where all non-alphanumeric characters except - _ . are replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs.

JavaScript has 2 functions to emulate urlencode(), but both fall short…
The escape function encodes all non-alphanumeric characters except * @ / + – _ .
The encodeURIComponent function encodes all non-alphanumeric characters except spaces and ! ‘ ( ) * ~

The winner for me though is encodeURIComponent as it encodes all UTF-8 characters while escape only encodes ISO Latin characters.

With this in hand, mozilla help provide a neat function.
Continue reading

Post XML with HTTPS Authentication using PHP and cURL

Recently I needed to post XML to a particular API and this API required HTTPS authentication. So I needed to send a username / password with the request.

This was pretty straightforward in C#.NET ( at least from my foggy memory ). You needed to use NetworkCredentials. But it was a bit difficult to find a similar way of doing this in PHP… but eventually I found this gem.

This uses cURL which is just a library to send and receive data from remote sites. Now all I needed was a neat way of using cURL to post XML… and I found a neat implementation here.

Two plus two and you get …
Continue reading