File tree Expand file tree Collapse file tree 1 file changed +36
-1
lines changed Expand file tree Collapse file tree 1 file changed +36
-1
lines changed Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments