How do I grab the text after the location and output it? From this site: I want to put the visitor's IP address; and grab the location; http://www.hostip.info/ Thanks
Just use geoplugin. $fetch = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR'])); $country_code = $fetch['geoplugin_countryCode']; $city = $fetch['geoplugin_city']; $state = $fetch['geoplugin_region']; PHP: And so on, the other variables that you can use as keys in a return are: (these are based on my IP) FYI : KBlinker (in my sig) uses this method by default for geo-targeting.
I noticed host IP has it's own API in the same fashion, the best approach would be to use their XML url (http://api.hostip.info/?ip=xxx.xxx.xxx.xxx) But you'll need to parse the xml that gets returned (it's not quite as friendly as geoplugin where it returns a serialized string that can be immediately turned into a php array). If all you want is the country code using hostip $country_code = file_get_contents("http://api.hostip.info/country.php?ip=".$_SERVER['REMOTE_ADDR']); PHP:
file_get_contents(url) is not a great idea - most hosting companies have disallowed fopen on remote files. you can use curl or you can use geolocation js apis. for instance, i wrote one a while back: http://fragged.org/dev/geoinfo.php but this uses the mootools javascript framework. anyway, its basic enough to reproduce using vanilla js also, using geoip.pidgets.com, with a fallback to query.yahooapis.com that gets extended info: var geoData = new Class({ Implements: [Options, Events], options: { url: "http://geoip.pidgets.com/?format=json", callback: $empty, }, initialize: function(options) { this.setOptions(options); }, lookup: function() { new Asset.javascript(this.options.url + "&callback=" + this.options.callback); // console.log("done...", this.options.url + "&callback=" + this.options.callback); }, setData: function(data) { this.geoData = data; // console.log(data); this.fireEvent("complete", data); }, complete: function(data) { this.fireEvent("complete", data); } }); var county = city = false, myData = new geoData({ callback: "myData.setData" // needs to match instance name }).addEvents({ "complete": function(data) { // console.log(data); try { data.query.results.place.each(function(el) { if (el.admin2.type == "County" && !county) county = el.admin2.content; }); } catch (e) { // console.log("error", e); } this.setOptions({ url: 'http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text="'+data.city+'"&format=json', }); if (!county) { city = data.city; //console.log(this); this.lookup(); } else { $("output").set("html", "We think you may be in " +city + ", " + county); } } }); myData.lookup(); PHP: obviously, you can use ajax to send it back to your php etc.
Guys, this was a really helpful response, I found both, very helpful and it works beautifully! thanks a lot!
That is why I implemented this very small function in most of my codes. function fetch_url($url) { if (!function_exists('file_get_contents')) return file_get_contents_curl($url); else return file_get_contents($url); } function file_get_contents_curl($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); $data = curl_exec($ch); curl_close($ch); return $data; } PHP: