Fetchify Documentation
Fetchify
Sign Up
Contact
Fetchify
Sign Up
Contact
  • Getting Started
  • Integrations

    • BigCommerce
    • CS-Cart
    • Ecommerce Templates
    • EKM
    • Formstack
    • Magento 2
    • Microsoft Dynamics 365
    • NetSuite
    • nopCommerce
    • Odoo
    • OpenCart
    • osCommerce
    • PrestaShop
    • Salesforce
    • Shopify Plus
    • Shopware
    • VirtueMart
    • WooCommerce
    • Xero
    • Zen Cart
    • Zapier
    • Zoey
  • Browser Extension

    • Chrome Browser Extension Guide
    • Firefox Browser Extension Guide
  • JavaScript Library

    • Address Auto-Complete
    • UK Postcode Lookup
    • Phone Validation
    • Email Validation
    • Bank Validation
    • Tutorials

      • Address Auto-Complete Website Integration
      • Customising Address Auto-Complete
      • Address Auto-Complete On Two Forms
      • UK Postcode Lookup in 15 Minutes
      • UK Postcode Lookup On Two Forms (Billing & Shipping)
      • Using Address Auto-Complete in React
      • Using Address Auto-Complete in Vue
    • Sample Code

      • Client Side JavaScript Address Auto-Complete
      • JavaScript & HTML – Client Side Postcode Lookup
  • Address & Validation API

    • Address Auto-Complete
    • Phone Validation API
    • Email Validation API
    • Bank Validation API
  • UK Postcode Lookup API

    • UK Postcode Lookup
    • UK Postcode Lookup (XML)
    • UK Postcode Geocodes
    • Errors
    • Sample Code

      • Microsoft C# Wrapper
      • Microsoft C++ Example Application
      • JSON API integration with jQuery UI
      • PHP – Server Side UK Postcode Lookup
      • Python – UK Postcode Lookup Example
      • RealStudio/XOJO Example
      • Microsoft VB.Net Sample Code
  • Embedded Tech API

    • Embedded Tech API
  • End-of-Life Announcements

    • Plugins & Extensions

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 B

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

<?php
define ('CRAFTY_KEY',	'xxxxx-xxxxx-xxxxx-xxxxx');
define ('CRAFTY_URL',	'https://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.'; ';
	}
}
?>
Prev
JSON API integration with jQuery UI
Next
Python – UK Postcode Lookup Example