@@ -57,10 +57,6 @@ abstract public function getByIp($ip);
57
57
*/
58
58
public function performGeolocation ($ apiKey )
59
59
{
60
- if (function_exists ('curl_init ' ) == false ) {
61
- throw new Exception ('Curl must be enabled on the server ' );
62
- }
63
-
64
60
if (empty ($ apiKey )) {
65
61
return 'Empty API key. No geolocations performed ' ;
66
62
}
@@ -71,15 +67,25 @@ public function performGeolocation($apiKey)
71
67
$ location ->setLatitude ('0 ' );
72
68
$ location ->setLongitude ('0 ' ); // only try geolocation once per ip
73
69
if ($ location ->getIp ()) {
74
- $ url = 'http://api.ipinfodb.com/v3/ip-city/?key= ' .$ apiKey .'&ip= ' .$ location ->getIp ().'&format=json ' ;
70
+ $ url = 'https://api.ipinfodb.com/v3/ip-city/?key= ' .$ apiKey .'&ip= ' .$ location ->getIp ().'&format=json ' ;
71
+
72
+ if (extension_loaded ('curl ' )) {
73
+ $ curl = curl_init ();
74
+ curl_setopt ($ curl , CURLOPT_URL , $ url );
75
+ curl_setopt ($ curl , CURLOPT_RETURNTRANSFER , true );
76
+ curl_setopt ($ curl , CURLOPT_PORT , 443 );
77
+ $ response = curl_exec ($ curl );
78
+ $ status = curl_getinfo ($ curl , CURLINFO_HTTP_CODE );
75
79
76
- $ curl = curl_init ();
77
- curl_setopt ($ curl , CURLOPT_URL , $ url );
78
- curl_setopt ($ curl , CURLOPT_RETURNTRANSFER , true );
79
- $ resp = curl_exec ($ curl );
80
+ if ($ status != 200 ) {
81
+ $ response = false ;
82
+ }
83
+ } else {
84
+ $ response = file_get_contents ($ url , false );
85
+ }
80
86
81
- if ($ resp && ! empty ( $ resp ) ) {
82
- $ answer = json_decode ($ resp );
87
+ if ($ status !== false ) {
88
+ $ answer = json_decode ($ response );
83
89
if ($ answer && strtoupper ($ answer ->statusCode ) == 'OK ' ) {
84
90
$ location ->setLatitude ($ answer ->latitude );
85
91
$ location ->setLongitude ($ answer ->longitude );
0 commit comments