-4

I am using a geo location script on my site to create a country and city location that shows within a sentence whenever users visit my page. However the function is not working as intended and sometimes gives the wrong country and city.

This is code for the geo location script: <script>jQuery.ajax({url:"//freegeoip.net/json/",type:"POST",dataType:"jsonp",success:function(b){jQuery("#findcity").html(b.city);jQuery("#region-code").html(b.region_code);jQuery("#region-name").html(b.region_name);jQuery("#areacode").html(b.areacode);jQuery("#ip").html(b.ip);jQuery("#zipcode").html(b.zipcode);jQuery("#longitude").html(b.longitude);jQuery("#latitude").html(b.latitude);jQuery("#findcountry").html(b.country_name);jQuery("#country-code").html(b.country_code)}});</script>

The next line of code is to highlight the country and the city in a sentence in the body tag <h4 itemprop=headline align=center>Hello my <span id=findcountry></span> friend, how is <span id=findcity></span> today have got an awesome offer for you.</h4>

Currently freegeoip.net is where the country and city data is being called from but they are currently down often and cause a typo on the sentence because of this.

Is there a better solution out there?

AStopher
  • 4,207
  • 11
  • 50
  • 75
seowebanalyst
  • 19
  • 1
  • 8

1 Answers1

1

Try this code:

Note: create your api key from HERE

        $remoteIp = $_SERVER['HTTP_HOST'];
        $geoLocationUrl="http://api.db-ip.com/addrinfo?   addr=$remoteIp&api_key='YOUR API KEY'";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $geoLocationUrl);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

        $auth = curl_exec($curl);
        if($auth)
        {
        $json = json_decode($auth,true); 
        echo $address =  $json['address'];
         echo $country =$json['country'];
         echo $state =$json['stateprov'];
         echo $city =$json['city'];
        }
        else{
        echo 'ERRROR';

        }
Priyank
  • 3,778
  • 3
  • 29
  • 48
  • @user2366920 : let me know if you have any query regarding my code – Priyank Jan 07 '15 at 09:47
  • Please refrain from answering off-topic questions. – AStopher Jan 07 '15 at 10:04
  • @cybermonkey how is it off topic question? to ask for help with getting code? – seowebanalyst Jan 07 '15 at 10:39
  • @user2366920 That exactly. You should review [ask] for more information. – AStopher Jan 07 '15 at 10:48
  • @user2366920 If you had enough reputation I would start a chat with you and help you re-model your question. Get rid of your 'get me teh codez' aspect, paste your function that you've made and I'll edit it to make it a good question. – AStopher Jan 07 '15 at 10:52
  • @Priyank please have added the code and the API but need it to show the country and city on the webpage by highlighting this two in a sentence. How can I call them up? – seowebanalyst Jan 07 '15 at 10:53
  • @cybermonkey Hi have added more detail of the issue,but the thing is it is a code from freegeoip.net and they are currently down and service no longer stable so am looking for new one that can serve same purpose...this is why I have asked the question that way. Hope my newly edited question pass the question mark? – seowebanalyst Jan 07 '15 at 10:59
  • @user2366920 not getting what you are asking.plz clearify – Priyank Jan 07 '15 at 11:00
  • ok the code u gave have paste it into my header and added the API...now I want to know how to pull out the country and the city in the sentence "Hello my [COUNTRY]friend, how is[CITY] today? have got an awesome offer for you." what line of code am i to write for this to call out the country and city in the sentence respectively...Thanks. I also notice that the country are only two digits, eg US= UNITED STATES can't one get it to be in full like that? – seowebanalyst Jan 07 '15 at 11:13
  • @user2366920 pass this variable $country and $city. like: my $country friend how is $city today also see my updated answer.i put echo – Priyank Jan 07 '15 at 11:17
  • @user2366920 You can use `http://www.geoplugin.net/json.gp?ip=iphere` where `iphere` is the IP to get the information for. The API is completely free. – AStopher Jan 07 '15 at 11:20
  • @Priyank please visit [link](https://www.seowebanalyst.com)[link] the code not working. you can help provide correction. Thanks – seowebanalyst Jan 07 '15 at 11:24
  • @user2366920 I've made the edit, please accept it. – AStopher Jan 07 '15 at 11:25
  • @user2366920 what is not working? show me what are you done.it's perfectly working for me – Priyank Jan 07 '15 at 11:26
  • @cybermonkey where am i to accept not seen the request. – seowebanalyst Jan 07 '15 at 11:31
  • @Priyank please visit [here](https://www.seowebanalyst.com) to see it and check the code to get what have done. – seowebanalyst Jan 07 '15 at 11:32
  • @cybermonkey am not getting you with your own code...i have posted my freegeoip.net code and need something same as that. am i to replace the line of code to call to freegeoip.net with yours? please can explain this. Thanks – seowebanalyst Jan 07 '15 at 11:36
  • @cybermonkey have accepted review. – seowebanalyst Jan 07 '15 at 11:45
  • @user2366920 Look at the output it gives if you use one of my own server IPs, for example: http://www.geoplugin.net/json.gp?ip=178.159.7.21. I flagged the question for moderator attention so it can be reviewed and maybe re-opened. – AStopher Jan 07 '15 at 12:05
  • @cybermonkey what i need is something like the first example showing country and city...but the example only show country...and if am to add punctuation marks will it still work eg ? , . http://www.geoplugin.com/examples – seowebanalyst Jan 07 '15 at 12:21
  • tand also I think I need https for their url which is going to cost money per year. – seowebanalyst Jan 07 '15 at 12:24
  • have looked at your geoplugin and the db-ip.com i see yours has more function but it is hughly moentized....and its request are very low per day. I think the db-ip.com is close to the freegeoip function. – seowebanalyst Jan 07 '15 at 12:29
  • @Priyank please can I see a sample of what you have done with the code need something to show like first example...stating the users country and city/state in a sentence like this first sample http://www.geoplugin.com/examples – seowebanalyst Jan 07 '15 at 12:31
  • @user2366920 see this link https://db-ip.com/api/#php – Priyank Jan 07 '15 at 12:35
  • @user2366920 did you tried my answer which i mention above? – Priyank Jan 07 '15 at 12:38
  • @Priyank I tried it all nothing works pls check source code view-source:https://www.seowebanalyst.com/ – seowebanalyst Jan 07 '15 at 12:43
  • @user2366920 it's a php code,how i'll see in view source? – Priyank Jan 07 '15 at 12:46
  • @user2366920 where did you implement my code – Priyank Jan 07 '15 at 12:47
  • @Priyank to make it easy simply do a sample for me that states the sentence and I will simply copy it and put in my code...that solves it. don't u think? – seowebanalyst Jan 07 '15 at 13:06
  • @user2366920 ok gives me your api key and your email id – Priyank Jan 07 '15 at 14:56
  • @Priyank my api key is b445964ceb48a8db4020010ff262e2a434c50b98 my email tekkbastion@yahoo.com please I like to see steps how to place the code into my webpage and make it show country and city of the web visitors as explained above. You can provide a sample if possibble I ll appreciate that. Thanks – seowebanalyst Jan 09 '15 at 10:59
  • @user2366920 send your email id to send me file – Priyank Jan 09 '15 at 11:00
  • @user2366920 check your email :) – Priyank Jan 09 '15 at 11:21
  • @Priyank Sorry for the inconvenience. have found that wamp/xammp is used in windows server and am using a LAMP server. So, kindly verify the request once again from your end and let me know what you need exactly. Please explain it in detail via email. tnx – seowebanalyst Jan 09 '15 at 14:18
  • @Priyank have copied the file that you have provided to the document root of your server and run the PHP file. But, we are getting a white page as the result. Please verify this...is the suppose to happen? – seowebanalyst Jan 09 '15 at 14:51