PHP
Official PHP client for the ipdata API. If you have any issues or run into any bugs please open an issue at https://github.com/ipdata/php/issues/new.

Install

1
composer require ipdata/api-client
Copied!
The client is built upon PSR standards. We use PSR-18 for HTTP client and PSR-17 for RequestFactory. These needs to be passed to the client's constructor.
Example packages for PSR-17 and PSR-18:
1
composer require nyholm/psr7 symfony/http-client
Copied!
1
use Ipdata\ApiClient\Ipdata;
2
use Symfony\Component\HttpClient\Psr18Client;
3
use Nyholm\Psr7\Factory\Psr17Factory;
4
5
$httpClient = new Psr18Client();
6
$psr17Factory = new Psr17Factory();
7
$ipdata = new Ipdata('my_api_key', $httpClient, $psr17Factory);
Copied!

How to use

To send a geocode request you simply need to provide the IP address you are interested in.
1
$data = $ipdata->lookup('69.78.70.144');
2
echo json_encode($data, JSON_PRETTY_PRINT);
Copied!
The output will be the response from the API server with one additional status field.
1
{
2
"ip": "69.78.70.144",
3
"is_eu": false,
4
"city": null,
5
"region": null,
6
"region_code": null,
7
"country_name": "United States",
8
"country_code": "US",
9
"continent_name": "North America",
10
"continent_code": "NA",
11
"latitude": 37.751,
12
"longitude": -97.822,
13
"postal": null,
14
"calling_code": "1",
15
"flag": "https:\/\/ipdata.co\/flags\/us.png",
16
"emoji_flag": "\ud83c\uddfa\ud83c\uddf8",
17
"emoji_unicode": "U+1F1FA U+1F1F8",
18
"asn": {
19
"asn": "AS6167",
20
"name": "Cellco Partnership DBA Verizon Wireless",
21
"domain": "verizonwireless.com",
22
"route": "69.78.0.0\/16",
23
"type": "business"
24
},
25
"carrier": {
26
"name": "Verizon",
27
"mcc": "310",
28
"mnc": "004"
29
},
30
"languages": [
31
{
32
"name": "English",
33
"native": "English"
34
}
35
],
36
"currency": {
37
"name": "US Dollar",
38
"code": "USD",
39
"symbol": "quot;,
40
"native": "quot;,
41
"plural": "US dollars"
42
},
43
"time_zone": {
44
"name": "America\/Chicago",
45
"abbr": "CST",
46
"offset": "-0600",
47
"is_dst": false,
48
"current_time": "2020-01-25T06:14:37.081772-06:00"
49
},
50
"threat": {
51
"is_tor": false,
52
"is_proxy": false,
53
"is_anonymous": false,
54
"is_known_attacker": false,
55
"is_known_abuser": false,
56
"is_threat": false,
57
"is_bogon": false
58
},
59
"count": "6",
60
"status": 200
61
}
Copied!
If you are not interested in all the fields in the response, you may query only the fields you want.
1
$data = $ipdata->lookup('69.78.70.144', ['longitude', 'latitude', 'country_name']);
2
echo json_encode($data, JSON_PRETTY_PRINT);
Copied!
1
{
2
"longitude": -97.822,
3
"latitude": 37.751,
4
"country_name": "United States",
5
"status": 200
6
}
Copied!

Bulk request

If you want to look up multiple IPs at the same time you may use the bulkLookup() function.
1
$data = $ipdata->buildLookup(['1.1.1.1', '69.78.70.144'], ['longitude', 'latitude', 'country_name']);
2
echo json_encode($data, JSON_PRETTY_PRINT);
Copied!
1
{
2
"0": {
3
"longitude": 143.2104,
4
"latitude": -33.494,
5
"country_name": "Australia"
6
},
7
"1": {
8
"longitude": -97.822,
9
"latitude": 37.751,
10
"country_name": "United States"
11
},
12
"status": 200
13
}
Copied!

Available Fields

A list of all the fields returned by the API is maintained at Response Fields

Errors

A list of possible errors is available at Status Codes
Last modified 1yr ago