我在我公司的站點中有一個函數,它使用cgi.remote_host
變量調用hostip.info提供的API並返回orgin的國家/地區。這是應該的工作方式是:在ColdFusion中調用遠程API有時會導致超時
- 功能從API發現全國各地,
- 有違我們的本地數據庫查詢,以確定我們要轉送 用戶哪些網站,
- 將用戶重定向到相應的站點。
如果出於任何原因,我們無法將某個國家與我們的某個地區關聯,我們會將該用戶重定向到默認網站。
什麼實際上發生的是,偶爾,我們的服務器無法找到API,我們得到超時錯誤。
這是我們呼籲得到境內功能的代理:
<cffunction name="getGeoInfo" access="public" output="true" returntype="any" hint="call getGeoIP API and check country against DB">
<cfargument name="IPAddress" required="yes" type="string" />
<!--- Calling hostip API to get our Geo IP information --->
<cfhttp url="http://api.hostip.info/?ip=#arguments.IPAddress#" method="get" result="geoIP" />
<!--- Try to parse the file, if it can't parse, we don't create the variable --->
<cftry>
<cfset geoIPXML = xmlParse(geoIP.fileContent) />
<cfcatch type="any" />
</cftry>
<!--- If variable was created, perform the function to get territory info --->
<cfif isDefined("geoIPXML")>
<cfset countryAbbrev = geoIPXML.HostipLookupResultSet['gml:featureMember']['hostip']['countryabbrev'].xmlText />
<cfquery name="theTerritory" datasource="#request.dsource#">
SELECT territory
FROM countries
WHERE countryCode = <cfqueryparam value="#countryAbbrev#" cfsqltype="cf_sql_varchar" />
</cfquery>
<!--- If no record was found, set locale to default --->
<cfif theTerritory.recordcount EQ 0>
<cfset returnLocale = "default" />
<cfelse>
<!--- Otherwise set it to territory found in DB --->
<cfset returnLocale = theTerritory.territory />
</cfif>
<cfelse>
<!--- if no XML file was returned, set locale to default --->
<cfset returnLocale="default" />
</cfif>
<cfreturn returnLocale />
</cffunction>
缺少什麼我在這裏?我應該如何改變這個函數,以便如果http請求超時,我仍然返回我的默認值?
這很明顯。我應該想到會這樣做。謝謝。 – 2009-06-09 18:24:57