Solved

API query not working for some unknown reason

Posted on 2016-10-01
7
22 Views
Last Modified: 2016-10-12
Hi all, I have a particular API Query that isnt working...

	Set SessionLookup=oConn.Execute("SELECT * FROM site_sessions WHERE session_ID="&Session.SessionID)
	If Not SessionLookup.EOF Then
		Session_ID=SessionLookup("session_ID")
	Else
		Set oXMLHTTPLocIP=CreateObject("MSXML2.ServerXMLHTTP")
		LocIP="http://api.ipify.org/"
		oXMLHTTPLocIP.Open "GET",LocIP,False
		oXMLHTTPLocIP.Send
		If oXMLHTTPLocIP.Status="200" Then
			IP=oXMLHTTPLocIP.responseText
			Set oXMLHTTPLoc=CreateObject("MSXML2.ServerXMLHTTP")
			IP=oXMLHTTPLocIP.responseText
			LocAPI="http://ip-api.com/json/"&IP&"?fields=countryCode,city"
			oXMLHTTPLoc.Open "GET",LocAPI,False
			oXMLHTTPLoc.Send
			If oXMLHTTPLoc.Status="200" Then
				GetTextFromUrlLoc=oXMLHTTPLoc.responseText
				CityLoc=Replace(Split(Split(GetTextFromUrlLoc,",")(0),":")(1),"""","")
				CountryLoc=Replace(Replace(Split(Split(GetTextFromUrlLoc,",")(1),":")(1),"""",""),"}","")
			End If
		End If
		oConn.Execute("INSERT INTO site_sessions(session_ID,brand_ID,session_IP,session_geoCity,session_geoCountry,session_timezone,session_date,session_time) VALUES("&Session.SessionID&",'"&SubDomain("brand_ID")&"','"&IP&"','"&CityLoc&"','"&CountryLoc&"','"&Timezone()&"','"&Date()&"','"&Time()&"')")
		Set NewSession=oConn.Execute("SELECT @@IDENTITY AS New_Session_ID")
		Session_ID=Cstr(NewSession("New_Session_ID"))
	End If

Open in new window


I am currently in Malaysia and this API thinks im in Australia.. I am so confused!
0
Comment
Question by:Graeme
  • 4
  • 3
7 Comments
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
Well, it works from here:

Set httpIP=CreateObject("MSXML2.ServerXMLHTTP")
LocIP="http://api.ipify.org/"
httpIP.Open "GET",LocIP,False
httpIP.Send
WScript.Echo "httpIP status:", httpIP.Status
If httpIP.Status="200" Then
	IP=httpIP.responseText
	WScript.Echo "httpIP response:", httpIP.responseText
	
	Set httpLocation=CreateObject("MSXML2.ServerXMLHTTP")
	IP=httpIP.responseText
	LocAPI="http://ip-api.com/json/"&IP&"?fields=countryCode,city"
	httpLocation.Open "GET",LocAPI,False
	httpLocation.Send
	WScript.Echo "httpLocation status:", httpLocation.Status
	If httpLocation.Status="200" Then
		WScript.Echo "httpLocation response:", httpLocation.responseText
		GetTextFromUrlLoc=httpLocation.responseText		
		City = Replace(Split(Split(GetTextFromUrlLoc,",")(0),":")(1),"""","")
		Country = Replace(Replace(Split(Split(GetTextFromUrlLoc,",")(1),":")(1),"""",""),"}","")
		WScript.Echo "City: ", City
		WScript.Echo "Country: ", Country
	End If
End If

Open in new window


Run it on the command line using cscript.exe:
Capture.PNG
When it works, then add some debugging output to your ASP code.
0
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
Ah, well, then it seems to use an outdated IP to location database. I don't think there can be done anything about it.

Try a different one: ip-api

Set httpIP=CreateObject("MSXML2.ServerXMLHTTP")
LocIP="http://ip-api.com/json"
httpIP.Open "GET",LocIP,False
httpIP.Send
WScript.Echo "httpIP status:", httpIP.Status
If httpIP.Status="200" Then
	IP=httpIP.responseText
	WScript.Echo "httpIP response:", httpIP.responseText	
End If
	

Open in new window

0
 

Author Comment

by:Graeme
Comment Utility
Hi Ste5an, I have integrated the code, however it still shows at that I am in AU, not elsewhere
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
hmm, another interesting question is: What IP do you use? Maybe your provider is a global company who's reusing IP addresses on a global scale. In such a scenario a simple IP lookup cannot work..
0
 

Author Comment

by:Graeme
Comment Utility
I am currently in UAE (now), just in hotels so I wouldnt think so...
0
 

Accepted Solution

by:
Graeme earned 0 total points
Comment Utility
After some playing around, I have got this code to work:

Set oXMLHTTPLoc=CreateObject("MSXML2.ServerXMLHTTP")
LocAPI="http://ip-api.com/json/"&IP&"?fields=countryCode,city"
oXMLHTTPLoc.Open "GET",LocAPI,False
oXMLHTTPLoc.Send
If oXMLHTTPLoc.Status="200" Then
	GetTextFromUrlLoc=oXMLHTTPLoc.responseText
	CityLoc=Replace(Split(Split(GetTextFromUrlLoc,",")(0),":")(1),"""","")
	CountryLoc=Replace(Replace(Split(Split(GetTextFromUrlLoc,",")(1),":")(1),"""",""),"}","")
End If

Open in new window

0
 

Author Closing Comment

by:Graeme
Comment Utility
self figured out
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now