Jumba' Posted January 16, 2010 Share Posted January 16, 2010 Hi, so uhm, I was trying to use the PHP SDK to get some IP geolocation information and sending it back to the server, but I ran into a problem I could not solve. Whenever I use the "doReturn" function I got errors ingame, upon investigating I realized that tables were being sent, instead of strings. Directly running the php script in the server returned this. [{"ip":"74.125.45.100","country_code":{"0":"US"},"country_name":{"0":"United States"},"region_name":{"0":"California"},"city":{"0":"Mountain View"},"zippostalcode":{"0":"94043"},"latitude":{"0":"37.4192"},"longitude":{"0":"-122.057"},"timezone":{"0":"-8"},"gmtoffset":{"0":"-8"},"dstoffset":{"0":"-7"}}] Only the IP is successfully sent to the server, the rest all appear as tables, but when I do "#table" it returns 0, and looping over the tables don't return any results either. I asked someone who knew a bit more of PHP then me and he said that "country_code":{"0":"US"} is a JSON object, but I thought the PHP SDK returns it as a normal string? Either way, no matter what I do it still fails This is my PHP file btw. <?php include ( "mta_sdk.php" ); function locateIp($ip){ $d = file_get_contents("http://www.ipinfodb.com/ip_query.php?ip=$ip&output=xml"); //Use backup server if cannot make a connection if (!$d){ $backup = file_get_contents("http://backup.ipinfodb.com/ip_query.php?ip=$ip&output=xml"); $answer = new SimpleXMLElement($backup); if (!$backup) return false; // Failed to open connection }else{ $answer = new SimpleXMLElement($d); } $country_code = $answer->CountryCode; $country_name = $answer->CountryName; $region_name = $answer->RegionName; $city = $answer->City; $zippostalcode = $answer->ZipPostalCode; $latitude = $answer->Latitude; $longitude = $answer->Longitude; $timezone = $answer->Timezone; $gmtoffset = $answer->Gmtoffset; $dstoffset = $answer->Dstoffset; //Return the data as an array return array('ip' => $ip, 'country_code' => $country_code, 'country_name' => $country_name, 'region_name' => $region_name, 'city' => $city, 'zippostalcode' => $zippostalcode, 'latitude' => $latitude, 'longitude' => $longitude, 'timezone' => $timezone, 'gmtoffset' => $gmtoffset, 'dstoffset' => $dstoffset); } //$input = mta::getInput(); //$ip_data = locateIp($input); $ip = "74.125.45.100"; $ip_data = locateIp($ip); mta::doReturn( $ip_data ); echo " Country code : " . $ip_data['ip'] . " "; echo "Country name : " . $ip_data['country_name'] . " "; echo "Region name : " . $ip_data['region_name'] . " "; echo "City : " . $ip_data['city'] . " "; echo "Zip/postal code : " . $ip_data['zippostalcode'] . " "; echo "Latitude : " . $ip_data['latitude'] . " "; echo "Longitude : " . $ip_data['longitude'] . " "; echo "Timezone : " . $ip_data['timezone'] . " "; echo "GmtOffset : " . $ip_data['gmtoffset'] . " "; echo "DstOffset : " . $ip_data['dstoffset'] . " "; ?> The lower part is commented out when I'm using callRemote, so that's not the problem. And that returns [{"ip":"74.125.45.100","country_code":{"0":"US"},"country_name":{"0":"United States"},"region_name":{"0":"California"},"city":{"0":"Mountain View"},"zippostalcode":{"0":"94043"},"latitude":{"0":"37.4192"},"longitude":{"0":"-122.057"},"timezone":{"0":"-8"},"gmtoffset":{"0":"-8"},"dstoffset":{"0":"-7"}}]Country code : 74.125.45.100 Country name : United States Region name : California City : Mountain View Zip/postal code : 94043 Latitude : 37.4192 Longitude : -122.057 Timezone : -8 GmtOffset : -8 DstOffset : -7 So it's something with the SDK, but I can't figure out what. Link to comment
Jumba' Posted January 16, 2010 Author Share Posted January 16, 2010 Hi, so uhm, I was trying to use the PHP SDK to get some IP geolocation information and sending it back to the server, but I ran into a problem I could not solve. Whenever I use the "doReturn" function I got errors ingame, upon investigating I realized that tables were being sent, instead of strings. Directly running the php script in the server returned this. [{"ip":"74.125.45.100","country_code":{"0":"US"},"country_name":{"0":"United States"},"region_name":{"0":"California"},"city":{"0":"Mountain View"},"zippostalcode":{"0":"94043"},"latitude":{"0":"37.4192"},"longitude":{"0":"-122.057"},"timezone":{"0":"-8"},"gmtoffset":{"0":"-8"},"dstoffset":{"0":"-7"}}] Only the IP is successfully sent to the server, the rest all appear as tables, but when I do "#table" it returns 0, and looping over the tables don't return any results either. I asked someone who knew a bit more of PHP then me and he said that "country_code":{"0":"US"} is a JSON object, but I thought the PHP SDK returns it as a normal string? Either way, no matter what I do it still fails This is my PHP file btw. <?php include ( "mta_sdk.php" ); function locateIp($ip){ $d = file_get_contents("http://www.ipinfodb.com/ip_query.php?ip=$ip&output=xml"); //Use backup server if cannot make a connection if (!$d){ $backup = file_get_contents("http://backup.ipinfodb.com/ip_query.php?ip=$ip&output=xml"); $answer = new SimpleXMLElement($backup); if (!$backup) return false; // Failed to open connection }else{ $answer = new SimpleXMLElement($d); } $country_code = $answer->CountryCode; $country_name = $answer->CountryName; $region_name = $answer->RegionName; $city = $answer->City; $zippostalcode = $answer->ZipPostalCode; $latitude = $answer->Latitude; $longitude = $answer->Longitude; $timezone = $answer->Timezone; $gmtoffset = $answer->Gmtoffset; $dstoffset = $answer->Dstoffset; //Return the data as an array return array('ip' => $ip, 'country_code' => $country_code, 'country_name' => $country_name, 'region_name' => $region_name, 'city' => $city, 'zippostalcode' => $zippostalcode, 'latitude' => $latitude, 'longitude' => $longitude, 'timezone' => $timezone, 'gmtoffset' => $gmtoffset, 'dstoffset' => $dstoffset); } //$input = mta::getInput(); //$ip_data = locateIp($input); $ip = "74.125.45.100"; $ip_data = locateIp($ip); mta::doReturn( $ip_data ); echo " Country code : " . $ip_data['ip'] . " "; echo "Country name : " . $ip_data['country_name'] . " "; echo "Region name : " . $ip_data['region_name'] . " "; echo "City : " . $ip_data['city'] . " "; echo "Zip/postal code : " . $ip_data['zippostalcode'] . " "; echo "Latitude : " . $ip_data['latitude'] . " "; echo "Longitude : " . $ip_data['longitude'] . " "; echo "Timezone : " . $ip_data['timezone'] . " "; echo "GmtOffset : " . $ip_data['gmtoffset'] . " "; echo "DstOffset : " . $ip_data['dstoffset'] . " "; ?> The lower part is commented out when I'm using callRemote, so that's not the problem. And that returns [{"ip":"74.125.45.100","country_code":{"0":"US"},"country_name":{"0":"United States"},"region_name":{"0":"California"},"city":{"0":"Mountain View"},"zippostalcode":{"0":"94043"},"latitude":{"0":"37.4192"},"longitude":{"0":"-122.057"},"timezone":{"0":"-8"},"gmtoffset":{"0":"-8"},"dstoffset":{"0":"-7"}}]Country code : 74.125.45.100 Country name : United States Region name : California City : Mountain View Zip/postal code : 94043 Latitude : 37.4192 Longitude : -122.057 Timezone : -8 GmtOffset : -8 DstOffset : -7 So it's something with the SDK, but I can't figure out what. Link to comment
50p Posted January 16, 2010 Share Posted January 16, 2010 (edited) You're sending back $ip_data which is a PHP array (you return it in locateIP function). Doesn't SDK convert PHP arrays to JSON objects and then sends it back? I'm pretty sure it does. Look into doReturn function in mta_sdk.php to be sure. That's why you get Lua table back, not a string. Why would it be string if you're sending an array? Edited January 17, 2010 by Guest Link to comment
50p Posted January 16, 2010 Share Posted January 16, 2010 (edited) You're sending back $ip_data which is a PHP array (you return it in locateIP function). Doesn't SDK convert PHP arrays to JSON objects and then sends it back? I'm pretty sure it does. Look into doReturn function in mta_sdk.php to be sure. That's why you get Lua table back, not a string. Why would it be string if you're sending an array? Edited January 17, 2010 by Guest Link to comment
Jumba' Posted January 17, 2010 Author Share Posted January 17, 2010 You're sending back $ip_data which is a PHP array (you return it in locateIP function). Doesn't SDK converts PHP arrays to JSON objects and then sends it back? I'm pretty sure it does. Look into doReturn function in mta_sdk.php to be sure.That's why you get Lua table back, not a string. Why would it be string if you're sending an array? Oh, sorry, I forgot to change the code, I just ran it with $ip_data to get the thing where it shows the json objects, when running it to connect with the server I use the $ip_data['ip'], $ip_data['country_name'] etc. This: mta::doReturn( $ip_data['ip'], $ip_data['country_name'], $ip_data['country_code'] ); Returns this: ["74.125.45.100",{"0":"United States"},{"0":"US"}] As you can see, only the ip is returned as a string, not the other stuff, and it's not a problem with the API I'm using since using "echo $ip_date['country_name'] works fine. Link to comment
Jumba' Posted January 17, 2010 Author Share Posted January 17, 2010 You're sending back $ip_data which is a PHP array (you return it in locateIP function). Doesn't SDK converts PHP arrays to JSON objects and then sends it back? I'm pretty sure it does. Look into doReturn function in mta_sdk.php to be sure.That's why you get Lua table back, not a string. Why would it be string if you're sending an array? Oh, sorry, I forgot to change the code, I just ran it with $ip_data to get the thing where it shows the json objects, when running it to connect with the server I use the $ip_data['ip'], $ip_data['country_name'] etc. This: mta::doReturn( $ip_data['ip'], $ip_data['country_name'], $ip_data['country_code'] ); Returns this: ["74.125.45.100",{"0":"United States"},{"0":"US"}] As you can see, only the ip is returned as a string, not the other stuff, and it's not a problem with the API I'm using since using "echo $ip_date['country_name'] works fine. Link to comment
50p Posted January 17, 2010 Share Posted January 17, 2010 It's probably SDK's problem then. Or the data you have in the array is another array. What does "$ip_data['country_name']" and "$ip_data['country_code']" show when you echo it and preview in browser? They seem to be arrays too. Link to comment
50p Posted January 17, 2010 Share Posted January 17, 2010 It's probably SDK's problem then. Or the data you have in the array is another array. What does "$ip_data['country_name']" and "$ip_data['country_code']" show when you echo it and preview in browser? They seem to be arrays too. Link to comment
Jumba' Posted January 17, 2010 Author Share Posted January 17, 2010 Using this echo " Country code : " . $ip_data['ip'] . " "; echo "Country name : " . $ip_data['country_name'] . " "; echo "Region name : " . $ip_data['region_name'] . " "; echo "City : " . $ip_data['city'] . " "; echo "Zip/postal code : " . $ip_data['zippostalcode'] . " "; echo "Latitude : " . $ip_data['latitude'] . " "; echo "Longitude : " . $ip_data['longitude'] . " "; echo "Timezone : " . $ip_data['timezone'] . " "; echo "GmtOffset : " . $ip_data['gmtoffset'] . " "; echo "DstOffset : " . $ip_data['dstoffset'] . " "; returns this. Country code : 74.125.45.100Country name : United States Region name : California City : Mountain View Zip/postal code : 94043 Latitude : 37.4192 Longitude : -122.057 Timezone : -8 GmtOffset : -8 DstOffset : -7 I've checked, it doesn't return an array. I've also tried retrieving the string using $ip_data['country_name']['0'] (and other variants), but it returns null. Link to comment
Jumba' Posted January 17, 2010 Author Share Posted January 17, 2010 Using this echo " Country code : " . $ip_data['ip'] . " "; echo "Country name : " . $ip_data['country_name'] . " "; echo "Region name : " . $ip_data['region_name'] . " "; echo "City : " . $ip_data['city'] . " "; echo "Zip/postal code : " . $ip_data['zippostalcode'] . " "; echo "Latitude : " . $ip_data['latitude'] . " "; echo "Longitude : " . $ip_data['longitude'] . " "; echo "Timezone : " . $ip_data['timezone'] . " "; echo "GmtOffset : " . $ip_data['gmtoffset'] . " "; echo "DstOffset : " . $ip_data['dstoffset'] . " "; returns this. Country code : 74.125.45.100Country name : United States Region name : California City : Mountain View Zip/postal code : 94043 Latitude : 37.4192 Longitude : -122.057 Timezone : -8 GmtOffset : -8 DstOffset : -7 I've checked, it doesn't return an array. I've also tried retrieving the string using $ip_data['country_name']['0'] (and other variants), but it returns null. Link to comment
50p Posted January 17, 2010 Share Posted January 17, 2010 doReturn method must be messed up then. EDIT: I just downloaded the SDK to check what's wrong with it and can't really see anything wrong. Link to comment
50p Posted January 17, 2010 Share Posted January 17, 2010 doReturn method must be messed up then. EDIT: I just downloaded the SDK to check what's wrong with it and can't really see anything wrong. Link to comment
Jumba' Posted January 17, 2010 Author Share Posted January 17, 2010 doReturn method must be messed up then.EDIT: I just downloaded the SDK to check what's wrong with it and can't really see anything wrong. mmkay, I'll just have to skip this idea then. Thanks for all the help though. Link to comment
Jumba' Posted January 17, 2010 Author Share Posted January 17, 2010 doReturn method must be messed up then.EDIT: I just downloaded the SDK to check what's wrong with it and can't really see anything wrong. mmkay, I'll just have to skip this idea then. Thanks for all the help though. Link to comment
Aibo Posted February 20, 2010 Share Posted February 20, 2010 try this way: return array($ip, $country_code, $country_name, $region_name, $city, $zippostalcode, $latitude, $longitude, $timezone, $gmtoffset, $dstoffset); and instead of mta::doReturn: echo json_encode($ip_data); Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now