Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-07 11:42:14
Original
1063 people have browsed it

新浪通过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元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!