PHP code example of hurnell / postcode-api-bundle
1. Go to this page and download the library: Download hurnell/postcode-api-bundle library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hurnell / postcode-api-bundle example snippets
# config/bundles.php
Hurnell\PostcodeApiBundle\HurnellPostcodeApiBundle::class => ['all' => true],
use Hurnell\PostcodeApiBundle\Service\PostcodeApiClient;
// use Exception classes
class MyController extends AbstractController {
public function getPostcodeAction(PostcodeApiClient $client){
$form = $this->createForm(PostcodeFormType::class);
try {
$postcodeModel = $client
->makeRequest(
'2011XC',
20,
'RD'
)
->populatePostcodeModel();
$postcodeModel->getStreet(); // Doelstraat
$postcodeModel->getCity(); // Haarlem
// $postcodeModel-> get etc etc
// json response
return $this->json($postcodeModel->toArray());
} catch (InvalidApiResponseException|InvalidPostcodeException $e) {
// handle exception
} catch (InvalidHouseNumberException $e) {
// handle exception
$form->get('number')->addError(new FormError($e->getMessage()));
} catch (InvalidNumberExtraException $e) {
// handle exception
$postcodeModel = $client->populatePostcodeModelWithoutExtra();
return $this->json(
array_merge(
$postcodeModel->toArray(),
['warning'=>$e->getMessage()]
)
);
}
}
}
try {
$postcodeModel = $client
->makeRequest(
'2011XC',
20,
'RD'
)
->populatePostcodeModel();
// ...
} catch (InvalidNumberExtraException $e) {
$postcodeModel = $client->populatePostcodeModelWithoutExtra();
return $this->json(
array_merge(
$postcodeModel->toArray(),
['warning'=>$e->getMessage()]
)
);
}