IP Call Sina API to get city name and convert to Pinyin

Source: Internet
Author: User
Tags curl

  This article mainly introduces the IP call Sina API to get the city name and converted to Pinyin example, the need for friends can refer to the following

Function:   1, get current IP address, 2, call Sina API, get to the current city. 3, convert the Chinese to pinyin and then jump.   Code as follows: <?php  include './pinyin.php ';  //Get current IP function GetIP () {  $ONLINEIP = ';   if (getenv (' http_client_ip ') &&strcasecmp getenv ( ' Http_client_ip '), ' unknown ') {    $onlineip =getenv (' http_client_ip ');  } elseif (getenv (' Http_x_ Forwarded_for ') &&strcasecmp (getenv (' http_x_forwarded_for '), ' unknown ')) {    $onlineip =getenv (' Http_x_forwarded_for ');  } elseif (getenv (' remote_addr ') &&strcasecmp (getenv (' remote_addr '), ' unknown ')) {   $ Onlineip=getenv (' remote_addr ');  } elseif (Isset ($_server[' remote_addr ')) &&$_server[' remote_addr ']&&strcasecmp ($_server[' Remote_ ADDR '], ' unknown ') {    $ONLINEIP =$_server[' remote_addr '];  }   return $ONLINEIP; &NBSP}    //get the city Information API  function GetLocation ($IP) {  $curl = Curl_init ();   curl_setopt ($curl, C Urlopt_url, "Http://int.dpool.sina.com.cn/iplookUp/iplookup.php?format=json&ip= ". $ip);   curl_setopt ($curl, Curlopt_returntransfer, 1);   curl_setopt ($curl, Curlopt_connecttimeout, 30);   $STR = curl_exec ($curl);   Curl_close ($curl);   return $STR; &NBSP}    //current IP address   $currentIP = GetIP ();    //through the current IP access information   $getLocation = getLocation ($currentIP);   $currentInfo = Json_decode ($getLocation, True);     //determine if IP is a valid  if ($currentInfo [' ret '] = = ' -1 ')  {  $currentInfo [' city '] = ' unknown '; &nbsp}    //current urban Chinese name   $currentCityName = $CURRENTINF o[' City '];     $currentCityEName = $pin->pinyin ("$currentCityName", ' UTF8 ');    //City Pinyin pronunciation  switch ($currentCityEName)  {  case ' zhongqing ':     $currentCityEName = ' Chongqing ';   break;     case ' Shenfang ':     $currentCityEName = ' Shifang ';   break;     case ' Chengdou ':     $currentCityEName = ' Chengdu '; &nbsp Break     case ' Yueshan ':     $currentCityEName = ' Leshan ';   break;     case ' Junxian ':     $currentCityEName = ' Xunxian ';    break;     case ' shamen ':     $currentCityEName = ' Xiamen ';    break;     case ' Zhangsha ':     $currentCityEName = ' Changsha ';    break;     case ' Weili ':     $currentCityEName = ' Yuli ';    break;     case ' Zhaoyang ':     $currentCityEName = ' Chaoyang ';    break;     case ' Danxian ':     $currentCityEName = ' Shanxian ';    break;     Default:     $currentCityEName = $pin->pinyin ("$currentCityName", ' UTF8 ');   break; &NBSP}    //redirect browser    header ("location:http://www.jb51.net");   exit;  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.