/** *$ip string 必傳 *獲取ip歸屬地 *demo 四川省成都市 電信 */ function get_ip_city($ip) { $ch = curl_init(); $url = 'https://whois.pconline.com.cn/ipJson.jsp?ip=' . $ip; //用curl發送接收數據 curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //請求為https curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $location = curl_exec($ch); curl_close($ch); //轉碼 $location = mb_convert_encoding($location, 'utf-8', 'GB2312'); //var_dump($location); //截取{}中的字符串 $location = substr($location, strlen('({') + strpos($location, '({'), (strlen($location) - strpos($location, '})')) * (-1)); //將截取的字符串$location中的‘,’替換成‘&’ 將字符串中的‘:‘替換成‘=’ $location = str_replace('"', "", str_replace(":", "=", str_replace(",", "&", $location))); //php內置函數,將處理成類似於url參數的格式的字符串 轉換成數組 parse_str($location, $ip_location); return $ip_location['addr']; }