How do I grab content from a page and display it?

Discussion in 'PHP' started by NewTier, Jul 24, 2009.

  1. #1
    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
     
    NewTier, Jul 24, 2009 IP
  2. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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.
     
    kblessinggr, Jul 24, 2009 IP
  3. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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:
     
    kblessinggr, Jul 24, 2009 IP
  4. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #4
    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.
     
    dimitar christoff, Jul 25, 2009 IP
    NewTier likes this.
  5. NewTier

    NewTier Notable Member

    Messages:
    2,201
    Likes Received:
    196
    Best Answers:
    0
    Trophy Points:
    250
    #5
    Guys, this was a really helpful response, I found both, very helpful and it works beautifully!

    thanks a lot!
     
    NewTier, Jul 25, 2009 IP
  6. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #6

    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:
     
    kblessinggr, Jul 25, 2009 IP