diff --git a/Example.php b/Example.php index 69fcefd..8337837 100644 --- a/Example.php +++ b/Example.php @@ -2,7 +2,8 @@ require_once 'vendor/autoload.php'; use \RapidWeb\Postcodes\Utils\Generator; use \RapidWeb\Postcodes\Utils\Validator; -use \RapidWeb\Postcodes\Objects\IdealPostCodes; +use \RapidWeb\Postcodes\Objects\IdealPostcodes; +use \RapidWeb\Postcodes\Objects\PostcodeAnywhere; $postcode = Generator::generatePostcode(); @@ -10,10 +11,12 @@ $validated = Validator::validatePostcode($postcode); -if ($validated) { +var_dump($validated); - $addresses = new IdealPostCodes('API_KEY'); +$idealPostcodes = new IdealPostCodes('API_KEY'); +$addresses = $idealPostcodes->getAddressesByPostcode('ST16 3DP'); +var_dump($addresses); - var_dump($addresses); - -} \ No newline at end of file +$postcodeAnywhere = new PostcodeAnywhere('API_KEY'); +$addresses = $postcodeAnywhere->getAddressesByPostcode('ST16 3DP'); +var_dump($addresses); diff --git a/README.md b/README.md index 428fed8..3a69d12 100644 --- a/README.md +++ b/README.md @@ -17,27 +17,22 @@ To install, just run the following composer command. ## Setup -### Ideal Postcodes +### Postcode Lookup Services -Using some of the data retrieval features provided by this library requires an Ideal Postcodes -API key. Sign up at https://ideal-postcodes.co.uk if you need to use these features. +Using some of the data retrieval features provided by this library requires a postcode lookup service. +It currently supports the following postcode lookup services. -You can then use the following code to get an `IdealPostcodes` object. +* Ideal Postcodes - https://ideal-postcodes.co.uk +* Postcode Anywhere (PCA Predict) - https://www.pcapredict.com/ -```php -$idealPostcodes = new \RapidWeb\Postcodes\Objects\IdealPostcodes('API_KEY'); -``` - - -### Postcode Anywhere (PCA Predict) - -Using some of the data retrieval features provided by this library requires an Postcode Anywhere (PCA Predict) -API key. Sign up at https://www.pcapredict.com/ if you need to use these features. +Sign up at the respective website if you need to use these features. -You can then use the following code to get an `PostcodeAnywhere` object. +You can then use the following code to get an appropriate postcode lookup service object. ```php -$postcodeAnywhere = new \RapidWeb\Postcodes\Objects\PostcodeAnywhere('API_KEY'); +$postcodeLookupService = new \RapidWeb\Postcodes\Objects\IdealPostcodes('API_KEY'); +// OR +$postcodeLookupService = new \RapidWeb\Postcodes\Objects\PostcodeAnywhere('API_KEY'); ``` ## Usage @@ -45,10 +40,10 @@ $postcodeAnywhere = new \RapidWeb\Postcodes\Objects\PostcodeAnywhere('API_KEY'); ### Get addresses by postcode To retrieve the addreses associated with a UK postcode, just pass it to the method shown below. -You will receive an array of addresses, appropriately split by their address lines. +You will receive an array of address objects, appropriately split by their address lines and other details. ```php -$addresses = $idealPostcodes->getAddressesByPostcode('ST163DP'); +$addresses = $postcodeLookupService->getAddressesByPostcode('ST163DP'); ``` ### Validate postcode diff --git a/pa-test.php b/pa-test.php deleted file mode 100644 index 4783c8a..0000000 --- a/pa-test.php +++ /dev/null @@ -1,8 +0,0 @@ -getAddressesByPostcode('st163jr'); \ No newline at end of file diff --git a/src/Objects/Address.php b/src/Objects/Address.php new file mode 100644 index 0000000..5238438 --- /dev/null +++ b/src/Objects/Address.php @@ -0,0 +1,14 @@ +result; + $addresses = []; + + foreach($object->result as $idealPostcodesAddress) { + $address = new Address; + $address->companyName = $idealPostcodesAddress->organisation_name; + $address->line1 = $idealPostcodesAddress->line_1; + $address->line2 = $idealPostcodesAddress->line_2; + $address->line3 = $idealPostcodesAddress->line_3; + $address->townCity = $idealPostcodesAddress->post_town; + $address->county = $idealPostcodesAddress->county; + $address->country = $idealPostcodesAddress->country; + $address->postcode = $idealPostcodesAddress->postcode; + $addresses[] = $address; + } + + return $addresses; } } \ No newline at end of file diff --git a/src/Objects/PostcodeAnywhere.php b/src/Objects/PostcodeAnywhere.php index 14065fe..ba6f69f 100644 --- a/src/Objects/PostcodeAnywhere.php +++ b/src/Objects/PostcodeAnywhere.php @@ -4,6 +4,7 @@ use GuzzleHttp\Client; use GuzzleHttp\Psr7\Response; use RapidWeb\Postcodes\Interfaces\PostcodeServiceInterface; +use RapidWeb\Postcodes\Objects\Address; use Exception; use SoapClient; @@ -11,6 +12,7 @@ class PostcodeAnywhere implements PostcodeServiceInterface { private $apiKey = null; private $findSOAPClient = null; + private $retrieveByIDSOAPClient = null; public function __construct($apiKey) { @@ -21,6 +23,7 @@ public function __construct($apiKey) $this->apiKey = $apiKey; $this->findSOAPClient = new SoapClient('https://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/Find/v1.10/wsdlnew.ws'); + $this->retrieveByIDSOAPClient = new \SoapClient('https://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/RetrieveById/v1.30/wsdlnew.ws'); } @@ -35,7 +38,32 @@ public function getAddressesByPostcode($postcode) $findResponseAddresses = $findResponse->PostcodeAnywhere_Interactive_Find_v1_10_Result->PostcodeAnywhere_Interactive_Find_v1_10_Results; - return $result; + $addresses = []; + + foreach($findResponseAddresses as $findResponseAddress) { + + $retrieveByIDResponse = $this->retrieveByIDSOAPClient->PostcodeAnywhere_Interactive_RetrieveById_v1_30( + [ + 'Key' => $this->apiKey, + 'Id' => $findResponseAddress->Id + ] + ); + + $retrieveAddress = $retrieveByIDResponse->PostcodeAnywhere_Interactive_RetrieveById_v1_30_Result->PostcodeAnywhere_Interactive_RetrieveById_v1_30_Results; + + $address = new Address; + $address->companyName = $retrieveAddress->Company; + $address->line1 = $retrieveAddress->Line1; + $address->line2 = $retrieveAddress->Line2; + $address->line3 = $retrieveAddress->Line3; + $address->townCity = $retrieveAddress->PostTown; + $address->county = $retrieveAddress->County; + $address->postcode = $retrieveAddress->Postcode; + $address->country = $retrieveAddress->CountryName; + $addresses[] = $address; + } + + return $addresses; } } \ No newline at end of file