Looking to get a php api that takes a postcode. goes too my database and brings back customer details for that post code.
A similar api is written like this so most of code can be copied. Simple project and i need this done today
public function geteircode(){
if(isset($_POST['eircode']) && !empty($_POST['eircode'])){
$code = strtr($_POST['eircode'],[' '=>'']);
$ir_identifiser = array("A41","A42","A45","A63","A67","A75","A81","A82","A83","A84","A85","A86","A91","A92","A94","A96","A98","C15","D01","D02","D03","D04","D05","D06","D6W","D07","D08","D09","D10","D11","D12","D13","D14","D15","D16","D17","D18","D20","D22","D24","E21","E25","E32","E34","E41","E45","E53","E91","F12","F23","F26","F28","F31","F35","F42","F45","F52","F56","F91","F92","F93","F94","H12","H14","H16","H18","H23","H53","H54","H62","H65","H71","H91","K32","K34","K36","K45","K56","K67","K78","N37","N39","N41","N91","P12","P14","P17","P24","P25","P31","P32","P36","P43","P47","P51","P56","P61","P67","P72","P75","P81","P85","R14","R21","R32","R35","R42","R45","R51","R56","R93","R95","T12","T23","T34","T45","T56","V14","V15","V23","V31","V35","V42","V92","V93","V94","V95","W12","W23","W34","W91","X35","X42","X91","Y14","Y21","Y25","Y34","Y35");
preg_match('/(A(4[125]|6[37]|75|8[123456]|9[12468])|C15|D(0[1-9]|1[0-8]|2[024]|6W)|(E(2[15]|3[24]|4[15]|53|91))|F(12|2[368]|3[15]|4[25]|5[26]|9[1-4])|H(1[2468]|23|5[34]|6[25]|[79]1)|K(3[246]|45|56|67|78)|N(3[79]|[49]1)|P(1[247]|2[45]|3[126]|4[37]|5[16]|6[17]|7[25]|8[15])|R(14|21|[34][25]|5[16]|9[35])|T(12|23|34|45|56)|V(1[45]|23|3[15]|42|9[2-5])|W(12|23|34|91)|X(35|42|91))\s?[0-9AC-FHKNPRTV-Y]{4}/i', $code, $output_array);
if(count($output_array)>0) {
$input = $output_array[0];
$eircode = str_replace($output_array[1],$output_array[1]." ",$input);
$eircode = str_replace(' ', '', $eircode);
$eircode = strtoupper($eircode);
}
if(in_array(substr($eircode,0,3),$ir_identifiser)) {
//$eircode = $eircode." ".$ll;
$eircode = $eircode;
$eircode = str_replace(' ', '', $eircode);
$eircode = strtoupper($eircode);
}else{
$eircode = '';
}
$mapsApiKey='AIzaSyB1DsbfwbSiKJmpR5aBmCxtErD54l177K0';
$query = "[login to view URL]".urlencode($eircode)."&key=".$mapsApiKey;
$data = file_get_contents($query);
// if data returned
if($data){
// convert into readable format
$data = json_decode($data,true);
//print_r(json_encode($data));
//exit;
$address=$data['predictions'][0]['description'];
$addressquery="[login to view URL]".urlencode($address)."&key=".$mapsApiKey;
$addressdata = file_get_contents($addressquery);
if($addressdata){
$addressdata = json_decode($addressdata,true);
$long_name='';
$locality='';
if(!empty($addressdata['results'][0]['address_components'])){
foreach ($addressdata['results'][0]['address_components'] as $address_componentsvalue) {
if(in_array("neighborhood", $address_componentsvalue['types'])){
$long_name=$address_componentsvalue['long_name'];
}
if(in_array("locality", $address_componentsvalue['types'])){
$locality=$address_componentsvalue['long_name'];
}
}
}
$address=$addressdata['results'][0]['formatted_address'];
$lat=$addressdata['results'][0]['geometry']['location']['lat'];
$long=$addressdata['results'][0]['geometry']['location']['lng'];
$origin_latlong = '51.88429199999999,-8.4672667';
$GPS_LATLONG = $lat.",".$long;
$getmapdata = '[login to view URL]'.urlencode($GPS_LATLONG).'&origin='.urlencode($origin_latlong).'&key='.$mapsApiKey;
$getjsondata = file_get_contents($getmapdata);
$mapjsondata = json_decode($getjsondata,true);
$distancevalue = '';$durationvalue = '';
if($mapjsondata['status'] == 'OK'){
$legs = $mapjsondata['routes'][0]['legs'][0];
$distancevalue = (int) round(trim(str_replace("km","",$legs['distance']['text'])));
//1 hour 41 mins
$dureationset = explode(" ",$legs['duration']['text']);
if(count($dureationset)>2){
$searchreplece = (int) ((((int) ($dureationset[0]))*60)+$dureationset[2]);
}
else {
$searchreplece = trim(str_replace("mins","",$legs['duration']['text']));
}
$durationvalue = (int) $searchreplece;
}
$return= array('Latitude'=>$lat,'Longitude'=>$long,'address'=>$address,'gps'=>$lat.",".$long,'long_name'=>$long_name,'locality'=>$locality,'distance'=>$distancevalue,'duration'=>$durationvalue);
$addressdata= json_encode($return);
}
}else{
$addressdata=$data;
}
echo $addressdata;
}else{
json_encode(array("error"=>"eircode not found"));
}
}