go 通过淘宝api,查询出口ip详细信息
2017-01-10 14:24
555 查看
package main import ( "encoding/json" "errors" "io/ioutil" "net" "net/http" "strings" log "qiniupkg.com/x/log.v7" "time" "fmt" "github.com/gin-gonic/gin" "mars.qiniu.com/collections" "mars.qiniu.com/models" ) // TaoBaoAPI to get the taobao api func TaoBaoAPI(ip string, ipj *models.IPJson) (err error) { fmt.Printf(ip) client := &http.Client{} request, _ := http.NewRequest("GET", "http://ip.taobao.com/service/getIpInfo.php?ip="+ip, nil) resp, err := client.Do(request) if err != nil { return } defer resp.Body.Close() data, err := ioutil.ReadAll(resp.Body) if err != nil { return } fmt.Printf(string(data[:])) return json.Unmarshal(data, ipj) } //IPGet is to get the ip by ip info func IPGet(c *gin.Context) { j := &models.IPJson{} ip, err := collections.IPMgr.Find(&models.IP{IP: ClientIP(c)}) if err != nil { if err.Error() == "not found" { if err := TaoBaoAPI(ClientIP(c), j); err != nil { log.Error(err.Error()) c.JSON(404, err) c.Abort() return } if j.Code == 1 { log.Error(errors.New("taobao not found")) c.JSON(404, err) c.Abort() return } if err := collections.IPMgr.Upsert(&j.Data); err != nil { log.Error(err.Error()) c.JSON(500, err) c.Abort() return } c.JSON(200, j.Data) return } log.Error(err.Error()) c.JSON(404, err) c.Abort() return } if (time.Now().Day() - ip.UpdatedAt.Day()) > 20 { if err := TaoBaoAPI(ClientIP(c), j); err != nil { log.Error(err.Error()) } else { if j.Code == 1 { log.Error(errors.New("taobao not found")) } else { if err := collections.IPMgr.Upsert(&j.Data); err != nil { log.Error(err.Error()) } else { c.JSON(200, j.Data) return } } } } c.JSON(200, ip) } //ClientIP is to get remote ip func ClientIP(c *gin.Context) string { clientIP := c.Request.Header.Get("X-Forwarded-For") if index := strings.IndexByte(clientIP, ','); index >= 0 { clientIP = clientIP[0:index] } clientIP = strings.TrimSpace(clientIP) if len(clientIP) > 0 { return clientIP } clientIP = strings.TrimSpace(c.Request.Header.Get("X-Real-Ip")) if len(clientIP) > 0 { c.JSON(200, gin.H{"ip": clientIP}) return clientIP } if ip, _, err := net.SplitHostPort(strings.TrimSpace(c.Request.RemoteAddr)); err == nil { return ip } return "127.0.0.1" }
相关文章推荐
- Java_调用HttpRequest访问淘宝开发API查询IP信息
- 通过JAVA bean查询ip归属地,身份证号码信息,手机号码归属地
- 淘宝API——获取IP归属地等信息
- 借助百度api通过经纬度查询位置信息
- 站台查询api 据站台名称查询站台详细信息
- 淘宝API——获取IP归属地等信息
- C++ 通过WIN32 API 获取逻辑磁盘详细信息
- C++ 通过WIN32 API 获取逻辑磁盘详细信息
- 通过IP获取对应城市信息(该功能基于淘宝第三方IP库接口)
- 苏州公交实时查询api 根据线路编码查询详细信息
- C++ 通过WIN32 API 获取逻辑磁盘详细信息
- python使用在线API查询IP对应的地理位置信息实例
- 淘宝IP地址库API接口(PHP)通过ip获取地址信息
- 怎么查询电脑mac地址,IP,DNS等详细信息
- 结合Google Maps API和GeoIP/新浪/搜狐接口的IP地理信息可视化查询
- java通过淘宝IP地址库获取详细的地域信息
- 根据淘宝ip数据库查询ip归属地信息
- C++通过WIN32 API获取逻辑磁盘详细信息
- PHP调用淘宝IP地址查询接口查询IP所在地信息实例