SandySynaptik Posted October 31, 2011 Share Posted October 31, 2011 I am using a web service called Geo I/O which lets me locate an IP address and get their Country, State, City and Latitudinal and Longitudinal status.The request that is sent is something like:http://api.geoio.com/q.php?key=YOUR TOKEN&qt=&d=comma&q=IP ADDRESS The above request sent gives a string like:Calcutta,West Bengal,India,Bcl East,22.5697,88.3697,IN Now I want the above string arranged something like this in the webpage:City: CalcuttaState: West BengalCountry: IndiaLatitude: 22.5697Longitude: 88.3697 Please help me out. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 31, 2011 Share Posted October 31, 2011 Use the explode function to split the string up on commas, you'll end up with an array with the different items in it. http://www.php.net/manual/en/function.explode.php Link to comment Share on other sites More sharing options...
SandySynaptik Posted November 1, 2011 Author Share Posted November 1, 2011 The problem is how do i sent this request for each of them who visits this page?Below is something I am up to: <!DOCTYPE html><html lang="en-us"><head><meta charset="utf-8"></head><body><?php$ip = $_SERVER["REMOTE_ADDR"];?></body></html> Link to comment Share on other sites More sharing options...
birbal Posted November 1, 2011 Share Posted November 1, 2011 You can use http://php.net/functon.file_get_contents or curl Link to comment Share on other sites More sharing options...
SandySynaptik Posted November 1, 2011 Author Share Posted November 1, 2011 What is wrong in this script? <?php echo $pieces[4], $pieces[5];?> I am trying to echo 2 variable in same line. Link to comment Share on other sites More sharing options...
Ingolme Posted November 1, 2011 Share Posted November 1, 2011 There shouldn'e be anything wrong with it. What happens when you try that code? Link to comment Share on other sites More sharing options...
SandySynaptik Posted November 1, 2011 Author Share Posted November 1, 2011 Oh ######! I posted wrong thing. Actually I was trying to put a comma and space between pieces[4] and pieces[5] and the code was something like this <?php echo $pieces[4],", " $pieces[5];?> Link to comment Share on other sites More sharing options...
Ingolme Posted November 1, 2011 Share Posted November 1, 2011 You're missing a comma between the string and the last variable:<?php echo $pieces[4], ", ", $pieces[5];?> Aside from that, the code should work fine. And what is displaying on the page when you use the code? Link to comment Share on other sites More sharing options...
SandySynaptik Posted November 1, 2011 Author Share Posted November 1, 2011 How can I cut down/shorten the following code? looks like I have used too many PHP tags. <!DOCTYPE html><html lang="en-us"><head> <meta charset="utf-8"></head><body><?php$IP = $_SERVER["REMOTE_ADDR"];$geodata = file_get_contents('http://api.geoio.com/q.php?key=6wVGBk7TshKizPJI&qt=geoip&d=comma&q=$IP');$pieces = explode(",", $geodata);echo $geodata;?><br />Your city : <?php echo $pieces[0];?><br />Your State: <?php echo $pieces[1];?><br />Your Country: <?php echo $pieces[2];?><br />Your Location (Lat): <?php echo $pieces[4];?><br />Your Location (Long): <?php echo $pieces[5];?><br />Your Location: <a href="http://maps.google.com/?q=<?php echo $pieces[4],", ", $pieces[5];?>"><?php echo $pieces[4],", ", $pieces[5];?></a><br />Your Country Code: <?php echo $pieces[6];?><br /><br />* I can't guaranty geolocational data, don't rely on them.</body></html> Link to comment Share on other sites More sharing options...
Ingolme Posted November 2, 2011 Share Posted November 2, 2011 It's just fine like that. There aren't too many PHP tags as long as there's something besides whitespace between the closing tag ?> and following opening tag <?php You could also put all the HTML into a string, if you wanted to. And use the heredoc syntax. <?php$IP = $_SERVER["REMOTE_ADDR"];$geodata = file_get_contents('http://api.geoio.com/q.php?key=6wVGBk7TshKizPJI&qt=geoip&d=comma&q=$IP');$pieces = explode(",", $geodata);echo $geodata;echo <<<HTMLCODE<br />Your city : $pieces[0]<br />Your State: {$pieces[1]}<br />Your Country: {$pieces[2]}<br />Your Location (Lat): {$pieces[4]}<br />Your Location (Long): {$pieces[5]}<br />Your Location: <a href="http://maps.google.com/?q={$pieces[4]}, {$pieces[5]}">{$pieces[4]}, {$pieces[5]}</a><br />Your Country Code: {$pieces[6]}<br /><br />HTMLCODE;?> Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.