1. Have you made sure you had all the required files?
2. Have you used a string to store the name of the country, city, etc? Please come with the code that you tried. |
GetCountry(playerid) { new country[32], message[128], name[24]; GetPlayerName(playerid, name, sizeof(name)); GetPlayerCountry(playerid, country, sizeof(country)) format(message, sizeof(message), "%s is from the country %s", name, country); return message; }
I will give you an example for this one geolocation.inc, since it is the one I've been using and it works.
Code:
GetCountry(playerid) { new country[32], message[128], name[24]; GetPlayerName(playerid, name, sizeof(name)); GetPlayerCountry(playerid, country, sizeof(country)) format(message, sizeof(message), "%s is from the country %s", name, country); return message; } |
stock GetPlayerCountry(playerid, string3123[], const len = sizeof(string3123)) { new ip[24]; GetPlayerIp(playerid, ip, sizeof(ip)); GetIPCountry(ip, string3123, len); return true; }
I will give you an example for this one geolocation.inc, since it is the one I've been using and it works.
Code:
GetCountry(playerid) { new country[32], message[128], name[24]; GetPlayerName(playerid, name, sizeof(name)); GetPlayerCountry(playerid, country, sizeof(country)) format(message, sizeof(message), "%s is from the country %s", name, country); return message; } |
stock GetPlayerCountry(playerid) { new string3123[32]; new ip123[24]; GetPlayerIp(playerid, ip123, sizeof(ip123)); GetIPCountry(ip123, string3123, sizeof(string3123)); return string3123; }
You can try one of the PHP location tracker and get the response from it..
PS When you say, its not "working", what do you mean? Whats the issue? |
//===[Misc]=====================================================================
#include <a_http>
#define GetPlayerHost(%1) LookupData[%1][Hostname]
#define GetPlayerISP(%1) LookupData[%1][ISP]
#define GetPlayerCountryCode(%1) LookupData[%1][Code]
#define GetPlayerCountryName(%1) LookupData[%1][Country]
#define GetPlayerCountryRegion(%1) LookupData[%1][Region]
#define IsProxyUser(%1) LookupData[%1][Proxy]
enum ldata
{
Hostname[60],
ISP[60],
Code[3],
Country[45],
Region[43],
Proxy
};
new
LookupData[MAX_PLAYERS][ldata],
PlayerSession[MAX_PLAYERS],
Retry[MAX_PLAYERS];
forward OnLookupResponse(sessionid, response, data[]);
forward OnLookupComplete(playerid);
//===[Callbacks]================================================================
public OnPlayerConnect(playerid)
{
cleardata(LookupData[playerid][Hostname]);
cleardata(LookupData[playerid][ISP]);
cleardata(LookupData[playerid][Code]);
cleardata(LookupData[playerid][Country]);
cleardata(LookupData[playerid][Region]);
LookupData[playerid][Proxy] = 0;
Retry[playerid] = 0;
PlayerSession[playerid] = 0;
LookupPlayerIP(playerid);
return CallLocalFunction("Lookup_OnPlayerConnect", "i", playerid);
}
public OnLookupResponse(sessionid, response, data[])
{
new xml[5][2],
playerid = GetPlayerFromSession(sessionid);
if(playerid == -1) return 1;
if(response != 200)
{
if(Retry[playerid] == 0)
{
Retry[playerid] = 1;
LookupPlayerIP(playerid);
}
return 1;
}
xml[0][0] = strfind(data, "<host>", true);
xml[0][1] = strfind(data, "</host>", true);
xml[1][0] = strfind(data, "<isp>", true);
xml[1][1] = strfind(data, "</isp>", true);
xml[2][0] = strfind(data, "<code>", true);
xml[2][1] = strfind(data, "</code>", true);
xml[3][0] = strfind(data, "<country>", true);
xml[3][1] = strfind(data, "</country>", true);
xml[4][0] = strfind(data, "<region>", true);
xml[4][1] = strfind(data, "</region>", true);
strmidex(LookupData[playerid][Hostname], data, 6 + xml[0][0], xml[0][1], 60);
strmidex(LookupData[playerid][ISP], data, 5 + xml[1][0], xml[1][1], 60);
strmidex(LookupData[playerid][Code], data, 6 + xml[2][0], xml[2][1], 3);
strmidex(LookupData[playerid][Country], data, 9 + xml[3][0], xml[3][1], 40);
strmidex(LookupData[playerid][Region], data, 8 + xml[4][0], xml[4][1], 40);
LookupData[playerid][Proxy] = strval(data[strfind(data, "<proxy>", true) + 7]);
CallLocalFunction("OnLookupComplete", "i", playerid);
return 1;
}
//===[Functions]================================================================
stock LookupPlayerIP(playerid)
{
if(!IsPlayerNPC(playerid))
{
new ip[16], lQuery[60];
static
SessionIndex;
SessionIndex++;
PlayerSession[playerid] = SessionIndex;
GetPlayerIp(playerid, ip, sizeof(ip));
format(lQuery, sizeof(lQuery), "www.countryffs.com/api.php?ip=%s", ip);
HTTP(SessionIndex, HTTP_GET, lQuery, "", "OnLookupResponse");
}
}
stock GetPlayerFromSession(sessionid)
{
for(new i = 0; i < MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i) && PlayerSession[i] == sessionid)
return i;
}
return -1;
}
stock DisplayLookupData(playerid, targetid)
{
new lstr[300], boxname[40];
GetPlayerIp(targetid, boxname, sizeof(boxname));
format(boxname, sizeof(boxname), "IP Lookup: %s", boxname);
format(lstr, sizeof(lstr),
"Hostname: %s\n\
ISP: %s\n\
Country: %s (%s)\n\
State/Region: %s",
LookupData[targetid][Hostname],
LookupData[targetid][ISP],
LookupData[targetid][Country],
LookupData[targetid][Code],
LookupData[targetid][Region]);
ShowPlayerDialog(playerid, 30000, DIALOG_STYLE_MSGBOX, boxname, lstr, "Close", "");
}
stock strmidex(dest[], const src[], start, end, maxlength=sizeof dest)
{
if(end - start > 1)
strmid(dest, src, start, end, maxlength);
}
stock cleardata(src[])
{
src[0] = '?';
new c = 1;
while(src[c] != '\0')
{
src[c] = '\0';
c++;
}
}
//===[Hooking]==================================================================
#if defined _ALS_OnPlayerConnect
#undef OnPlayerConnect
#else
#define _ALS_OnPlayerConnect
#endif
#define OnPlayerConnect Lookup_OnPlayerConnect
forward Lookup_OnPlayerConnect(playerid);
//Get IP and send that data:
new string[45];
GetPlayerIp(playerid, string, 16);
format(string, sizeof(string), "ip-api.com/csv/%s?fields=3", string);
HTTP(playerid, HTTP_GET, string, "", "OnLookupIP");
//Retrieve the data:
foward OnLookupIP(index, response_code, data[]);
public OnLookupIP(index, response_code, data[]) //Not checking if response was OK (200) here
{
new Country[64], cCode[2];
sscanf(data, "p<,>s[64]s[2]", Country, cCode);
//Now you know the country (Country) and country code (cCode) of "index" (index = playerid)
}
success,COUNTRY_NAME,COUNTRY_CODE(2 chars),REGION_CODE(2 chars),REGION_NAME,CITY,LAT,LON,TIMEZONE,ISP,ORG,AS,QUERY(which is the IP)
fail,message,query
//Get IP and send that data:
new string[37];
GetPlayerIp(playerid, string, 16);
format(string, sizeof(string), "ip-api.com/csv/%s", string);
HTTP(playerid, HTTP_GET, string, "", "OnLookupIP");
foward OnLookupIP(index, response_code, data[]);
public OnLookupIP(index, response_code, data[])
{
new success[8], Country[64], cCode[2], rCode[2], Region[64], City[64], Float:gps_lat, Float:gps_lon, timezone[64], IspName[64], IspOrg[64], AS[10];
sscanf(data, "p<,>s[8]s[64]s[2]s[2]s[64]s[64]ffs[64]s[64]s[64]s[10]", success, Country, cCode, rCode, Region, City, gps_lat, gps_lon, timezone, IspName, IspOrg, AS);
}