PHP – Server Side UK Postcode Lookup

An example of calling our XML API from PHP. It shows how to lookup a postcode, fetch and process the address data.

Download the code:

Download “PHP Server Side Integration” simple_php_xml_example.zip – 888 Bopen in new window

The code is very simple, it could be translated into any programming language:

<?php
define ('CRAFTY_KEY',	'xxxxx-xxxxx-xxxxx-xxxxx');
define ('CRAFTY_URL',	'http://pcls1.craftyclicks.co.uk/xml/rapidaddress');

$postcode = 'AA11AA';
// try 2 response styles
$response_style = 'data_formatted';
//$response_style = 'paf_compact';

$url = CRAFTY_URL.'?key='.CRAFTY_KEY.'&postcode='.$postcode.'&response='.$response_style;

if ($response_style == 'data_formatted') {
	// optional, default is 2, sets number of address lines
	$url.='&lines=3';
}

echo 'connect to : '.$url.'<br/><br/>';

$xml_data = simplexml_load_file ($url); // NOTE - your PHP config may not allow this, use cURL then.
//print_r($xml_data);

echo 'Response....<br/><br/>';

if ($response_style == 'paf_compact') {
	print_xml_data_compact_paf($xml_data);
} else {
	print_xml_data_formatted($xml_data);
}

echo '<br/><br/>done....<br/><br/>';
die(0);

function print_xml_data_compact_paf($xml_data) {
	$result = $xml_data->address_data_paf_compact;
	echo 'Locality :<br/>';
	print_one_item($result->double_dependent_locality);
	print_one_item($result->dependent_locality);
	print_one_item($result->town);
	print_one_item($result->postal_county);
	print_one_item($result->traditional_county);
	print_one_item($result->postcode);
	echo '<br/><br/>'.$result->thoroughfare_count.' street(s) found:<br/><br/>';
	foreach ($result->thoroughfare as $thoroughfare) {
		print_one_item($thoroughfare->dependent_thoroughfare_name);
		print_one_item($thoroughfare->dependent_thoroughfare_descriptor);
		print_one_item($thoroughfare->thoroughfare_name);
		print_one_item($thoroughfare->thoroughfare_descriptor);
		echo '<br/>this street has '.$thoroughfare->delivery_point_count.' delivery point(s):<br/>';
		foreach ($thoroughfare->delivery_point as $del_point) {
			print_one_item($del_point->department_name);
			print_one_item($del_point->organisation_name);
			print_one_item($del_point->po_box_number);
			print_one_item($del_point->building_number);
			print_one_item($del_point->sub_building_name);
			print_one_item($del_point->building_name);
			echo '<br/>';
		}
	}
}

function print_xml_data_formatted($xml_data) {
	$result = $xml_data->address_data_formatted;
	echo 'Locality :<br/>';
	print_one_item($result->town);
	print_one_item($result->postal_county);
	print_one_item($result->traditional_county);
	print_one_item($result->postcode);
	echo '<br/><br/>'.$result->delivery_point_count.' delivery point(s) found:<br/><br/>';
	foreach ($result->delivery_point as $del_point) {
		print_one_item($del_point->department_name);
		print_one_item($del_point->organisation_name);
		print_one_item($del_point->line_1);
		print_one_item($del_point->line_2);
		print_one_item($del_point->line_3);
		echo '<br/>';
	}
}

function print_one_item($txt) {
	if ('' != $txt) {
		echo $txt.'; ';
	}
}
?>