FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
php-client/Client/PostcodeClient.php at master · api-postcode/php-client · GitHub
api-postcode
/
php-client
Public
Notifications
You must be signed in to change notification settings
Fork
3
Star
9
Code
Issues
0
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
php-client
/
Client
/
PostcodeClient.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
80 lines (66 loc) · 2.14 KB
Breadcrumbs
php-client
/
Client
/
PostcodeClient.php
Copy path
File metadata and controls
80 lines (66 loc) · 2.14 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/*
* (c) Wessel Strengholt <wessel.strengholt@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace
ApiPostcode
\
Client
;
use
ApiPostcode
\
Exception
\
InvalidPostcodeException
;
use
ApiPostcode
\
Exception
\
InvalidResponseException
;
use
ApiPostcode
\
Model
\
Address
;
/**
* Class PostcodeClient
*
* @author Api Postcode <info@api-postcode.nl>
*/
class
PostcodeClient
{
/** @var string */
private
$
token
;
/**
* @param string $token
*/
public
function
__construct
(
$
token
)
{
$
this
->
token
=
$
token
;
}
/**
* @param string $zipCode
* @param string $houseNumber
*
* @throws InvalidPostcodeException
* @throws InvalidResponseException
*
* @return Address
*/
public
function
fetchAddress
(
$
zipCode
,
$
houseNumber
):
Address
{
if
(
0
===
preg_match
(
'
/^[1-9]{1}[0-9]{3}[\s]{0,1}[a-z]{2}$/i
'
,
$
zipCode
)) {
throw
new
InvalidPostcodeException
(
'
Given postcode incorrect
'
);
}
$
uri
=
sprintf
(
"
https://json.api-postcode.nl?postcode=%s&number=%s
"
,
$
zipCode
,
$
houseNumber
);
$
curl
=
curl_init
();
curl_setopt
(
$
curl
,
CURLOPT_URL
,
$
uri
);
curl_setopt
(
$
curl
,
CURLOPT_POST
,
1
);
curl_setopt
(
$
curl
,
CURLOPT_RETURNTRANSFER
,
true
);
curl_setopt
(
$
curl
,
CURLOPT_HTTPHEADER
, [
sprintf
(
'
Token: %s
'
,
$
this
->
token
)]);
$
serverOutput
=
curl_exec
(
$
curl
);
$
httpCode
=
curl_getinfo
(
$
curl
,
CURLINFO_HTTP_CODE
);
if
(
$
httpCode
!==
200
) {
throw
new
InvalidResponseException
(
'
Response does not return a valid response code
'
);
}
curl_close
(
$
curl
);
$
responseData
=
json_decode
(
$
serverOutput
,
true
);
$
address
=
new
Address
(
$
responseData
[
'
street
'
],
$
responseData
[
'
postcode
'
],
$
responseData
[
'
house_number
'
],
$
responseData
[
'
city
'
]
);
$
address
->
setLatitude
(
$
responseData
[
'
latitude
'
]);
$
address
->
setLongitude
(
$
responseData
[
'
longitude
'
]);
$
address
->
setProvince
(
$
responseData
[
'
province
'
]);
return
$
address
;
}
}
Back
|
FazBrowse Home
|
New Git URL