Skip to content

Commit 791b3ab

Browse files
committed
Added multiple tries
1 parent f1bd90f commit 791b3ab

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/Services/IPApi.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ class IPApi extends AbstractService
2222
*/
2323
protected $continents;
2424

25+
26+
/**
27+
* Values to try content.
28+
*
29+
* @var integer
30+
*/
31+
protected $tries = 3;
32+
33+
2534
/**
2635
* The "booting" method of the service.
2736
*
@@ -70,8 +79,34 @@ public function locate($ip)
7079
// Parse body content
7180
$json = json_decode($data[0]);
7281

82+
83+
# Has data
84+
if(!$json) {
85+
$this->tries = $this->tries-1; #2(1) 1(2)
86+
if($this->tries < 0) {
87+
88+
# requests fails, give empty values back
89+
return $this->hydrate([
90+
'ip' => $ip,
91+
'iso_code' => null,
92+
'country' => null,
93+
'city' => null,
94+
'state' => null,
95+
'state_name' => null,
96+
'postal_code' => null,
97+
'lat' => null,
98+
'lon' => null,
99+
'timezone' => null,
100+
'continent' => $this->getContinent(null)
101+
]);
102+
}
103+
104+
# try same request again
105+
return $this->locate($ip);
106+
}
107+
73108
// Verify response status
74-
if ($json && $json->status !== 'success') {
109+
if ($json->status !== 'success') {
75110
throw new Exception('Request failed (' . $json->message . ')');
76111
}
77112

0 commit comments

Comments
 (0)