-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClient.php
123 lines (103 loc) · 3.26 KB
/
Client.php
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* Created by PhpStorm.
* User: ignatenkovnikita
* Date: 28/08/16
* Time: 16:01
*/
namespace ignatenkovnikita\dadata;
use yii\base\Component;
class Client extends Component
{
// todo refactoring all component
public $token;
public $secret;
public $query_path = 'http://suggestions.dadata.ru';
const GET = 1;
const POST = 2;
private function send_request($path, $type, $params)
{
$ch = curl_init();
$url = $this->query_path . $path;
if ($type == self::GET) {
$url = $url . '?' . http_build_query($params);
}
curl_setopt($ch, CURLOPT_URL, $url);
// curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
if ($type == self::POST) {
curl_setopt($ch, CURLOPT_POST, 1);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Token ' . $this->token,
'X-Secret: ' . $this->secret
]);
if ($type == self::POST) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
}
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
}
public function getCityOrRegion($query)
{
$params = [
'query' => $query,
'from_bound' => ['value' => 'region'],
'to_bound' => ['value' => 'city']
];
return $this->send_request('/suggestions/api/4_1/rs/suggest/address', self::POST, $params);
}
public function getCityOrSettlement($query)
{
$params = [
'query' => $query,
'from_bound' => ['value' => 'city'],
'to_bound' => ['value' => 'settlement']
];
return $this->send_request('/suggestions/api/4_1/rs/suggest/address', self::POST, $params);
}
public function get_address($address)
{
return $this->send_request('/suggestions/api/4_1/rs/suggest/address', self::POST, ['query' => $address]);
}
public function get_geo($IP_address)
{
return $this->send_request('/detectAddressByIp', self::GET, ['ip' => $IP_address]);
}
/**
* @param $fiasId
* @return Dadata
*/
public function getLocationById($fiasId)
{
$this->query_path = 'https://dadata.ru';
$request = $this->send_request('/api/v2/findById/address', self::POST, ['query' => $fiasId]);
if (isset($request->suggestions[0])) {
return DadataModel::fromDadataSuggestion($request->suggestions[0]);
}
return false;
}
/**
* @param $ip
*
* @return bool|Dadata
*/
public function getLocationByIp($ip)
{
$this->query_path = 'https://dadata.ru';
$response = $this->send_request('/api/v2/detectAddressByIp', self::GET, ['ip' => $ip]);
if (!empty($response->location)) {
return DadataModel::fromDadataSuggestion($response->location);
}
return false;
}
public function getLocationUser(){
$ip = \Yii::$app->request->userIP;
return $this->getLocationByIp($ip);
}
}