@@ -57,10 +57,6 @@ abstract public function getByIp($ip);
5757 */
5858 public function performGeolocation ($ apiKey )
5959 {
60- if (function_exists ('curl_init ' ) == false ) {
61- throw new Exception ('Curl must be enabled on the server ' );
62- }
63-
6460 if (empty ($ apiKey )) {
6561 return 'Empty API key. No geolocations performed ' ;
6662 }
@@ -71,15 +67,25 @@ public function performGeolocation($apiKey)
7167 $ location ->setLatitude ('0 ' );
7268 $ location ->setLongitude ('0 ' ); // only try geolocation once per ip
7369 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 );
7579
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+ }
8086
81- if ($ resp && ! empty ( $ resp ) ) {
82- $ answer = json_decode ($ resp );
87+ if ($ status !== false ) {
88+ $ answer = json_decode ($ response );
8389 if ($ answer && strtoupper ($ answer ->statusCode ) == 'OK ' ) {
8490 $ location ->setLatitude ($ answer ->latitude );
8591 $ location ->setLongitude ($ answer ->longitude );
0 commit comments