首頁  >  文章  >  php教程  >  新浪通过IP地址获取当前地理位置

新浪通过IP地址获取当前地理位置

WBOY
WBOY原創
2016-06-07 11:42:141048瀏覽

新浪通过IP地址获取当前地理位置(ip端、国家、省份、城市、运营商)的php接口很强大呢
/**<br>  * 取得地理位置信息<br>  * $format : js json string<br>  * return $array<br>  */<br> function iplookup($format='json'){<br>     //返回Sina地理位置信息<br>     $res = restRequest('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format='.$format);<br>     $res = json_decode($res,true);<br>     return $res;<br> }/**<br>  * 调用REST服务<br>  */<br> function restRequest($url, $method = 'GET', $data, $contentType = 'application/json', $timeout = 30, $proxy = false) {<br>     $ch = null;<br>     if ('POST' === strtoupper($method)) {<br>         $ch = curl_init($url);<br>         curl_setopt($ch, CURLOPT_POST, 1);<br>         curl_setopt($ch, CURLOPT_HEADER,0 );<br>         curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);<br>         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br>         curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);<br>         curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);<br>         if($contentType) {<br>             curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$contentType));<br>         }<br>         if(is_string($data)){<br>             curl_setopt($ch, CURLOPT_POSTFIELDS, $data);<br>         } else {<br>             curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));<br>         }<br>     } elseif('GET' === strtoupper($method)) {<br>         if(is_string($data)) {<br>             $real_url = $url. (strpos($url, '?') === false ? '?' : ''). $data;<br>         } else {<br>             $real_url = $url. (strpos($url, '?') === false ? '?' : ''). http_build_query($data);<br>         }<br>         $ch = curl_init($real_url);<br>         curl_setopt($ch, CURLOPT_HEADER, 0);<br>         curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$contentType));<br>         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br>         curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);<br>     } else {<br>         $args = func_get_args();<br>         //Log::error($args);<br>         return false;<br>     }<br>     if ($proxy) {<br>         curl_setopt($ch, CURLOPT_PROXY, $proxy);<br>     }<br>     $ret = curl_exec($ch);<br>     $info = curl_getinfo($ch);<br>     $contents = array(<br>         'httpInfo' => array(<br>             'send' => $data,<br>             'url' => $url,<br>             'ret' => $ret,<br>             'http' => $info,<br>         )<br>     );<br>     curl_close($ch);<br>     return $ret;<br> }

AD:真正免费,域名+虚机+企业邮箱=0元

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn