Solved

API query not working for some unknown reason

Posted on 2016-10-01
7
37 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 34

Expert Comment

by:ste5an
ID: 41824863
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 34

Expert Comment

by:ste5an
ID: 41824868
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
ID: 41830036
Hi Ste5an, I have integrated the code, however it still shows at that I am in AU, not elsewhere
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Expert Comment

by:ste5an
ID: 41830088
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
ID: 41830255
I am currently in UAE (now), just in hotels so I wouldnt think so...
0
 

Accepted Solution

by:
Graeme earned 0 total points
ID: 41834359
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
ID: 41839778
self figured out
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

632 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