《PHP編程:PHP中實現(xiàn)獲取IP和地理位置類分享》要點:
本文介紹了PHP編程:PHP中實現(xiàn)獲取IP和地理位置類分享,希望對您有用。如果有疑問,可以聯(lián)系我們。
發(fā)現(xiàn)之前有一個腳本沒有寫完,簡單擴充了一下.
獲取IP使用的是經(jīng)典的邏輯,地輿定位使用的是SINA的通用接口.PHP學(xué)習(xí)
使用辦法詳見源碼:PHP學(xué)習(xí)
代碼如下:
<必修php
/*
?* GET LOCATION BY SINA IP INTERFACE
?*
?*????????????? @SOULTEARY 2013.04.14
?*/
classIP
{
??? private $args=array();
?
??? function__construct()
??? {
??????? $this->args=self::init_args(func_get_args());
??????? $ip=self::GetIP();
?
??????? $ret=preg_match_all('/(\d+\.){3}\d+/i',$ip,$result);
??????? if(!$ret){
??????????? returnfalse;
??????? }else{
??????????? $result=$result[0];
??????? }
?
??????? if(isset($this->args['ONLYIP'])&&$this->args['ONLYIP']==true){
?
??????????? if(isset($this->args['FORMAT'])&&$this->args['FORMAT']=='JSON'){
??????????????? $result=json_encode($result);
??????????? }else{
??????????????? $result=implode(',',$result);
??????????? }
??????????? if(isset($this->args['ECHO'])&&$this->args['ECHO']==true){
??????????????? echo$result;
??????????????? returntrue;
??????????? }else{
??????????????? return$result;
??????????? }
??????? }else{
?
??????????? $apiURL='http://int.dpool.sina.com.cn/iplookup/iplookup.php必修ip='.$result[0];
??????????? if(isset($this->args['FORMAT'])&&$this->args['FORMAT']=='JSON'){
??????????????? $apiURL.='&format=json';
??????????????? $return=$this->ipCURL($apiURL);
??????????? }else{
??????????????? $return=$this->ipCURL($apiURL);
??????????????? $return=iconv("GBK//IGNORE","UTF-8",$return);
??????????? }
?
??????????? if(isset($this->args['ECHO'])&&$this->args['ECHO']==true){
??????????????? echo$return;
??????????????? returntrue;
??????????? }else{
??????????????? return$return;
??????????? }
?
??????? }
?
??? }
?
??? public functioninit_args($args)
??? {
??????? $result=array();
??????? for($i=0,$n=count($args);$i<$n;$i++){
??????????? $result=self::associative_push($args[$i],$result);
??????? }
??????? return$result;
??? }
?
??? public functionassociative_push($arr,$tmp)
??? {
??????? if(is_array($tmp)){
??????????? foreach($tmpas$key=>$value){
??????????????? $arr[$key]=$value;
??????????? }
??????????? return$arr;
??????? }
??????? returnfalse;
??? }
?
??? public functionGetIP()
??? {
??????? if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])&&$_SERVER['HTTP_X_FORWARDED_FOR']&&strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'],'unknown')){
??????????? return$_SERVER['HTTP_X_FORWARDED_FOR'];
??????? }elseif(isset($_SERVER['REMOTE_ADDR'])&&$_SERVER['REMOTE_ADDR']&&strcasecmp($_SERVER['REMOTE_ADDR'],'unknown')){
??????????? return$_SERVER['REMOTE_ADDR'];
??????? }
??? }
?
??? private functionipCURL($url)
??? {
??????? $ch=curl_init();
??????? curl_setopt($ch,CURLOPT_URL,$url);
??????? curl_setopt($ch,CURLOPT_HEADER,0);
??????? ob_start();
??????? curl_exec($ch);
??????? curl_close($ch);
??????? $result=ob_get_contents();
??????? ob_end_clean();
??????? return$result;
??? }
}
?
必修>
<!DOCTYPE html>
<html>
<head>
??? <meta charset="utf8">
??? <title>demo</title>
??? <script type="text/javascript">
??????? varip="<必修php new IP(array('ONLYIP'=>true, 'ECHO'=>true));必修>";
??????? vardata=<必修php newIP(array('FORMAT'=>'JSON','ECHO'=>true));必修>;
??????? varresult='';
??????? for(oo indata){
??????????? result+=oo+':'+data[oo]+"\n";
??????? }
??????? alert(result+ip);
??? </script>
</head>
<body>
<h1>CODE:</h1>
?
<h2>GETIP</h2>
?
<p>'ONLYIP'=>true,'ECHO'=>true</p>
?
<p><必修phpnewIP(array('ONLYIP'=>true,'ECHO'=>true));必修></p>
?
<P>'ONLYIP'=>true,'FORMAT'=>'JSON','ECHO'=>true</P>
?
<p><必修phpnewIP(array('ONLYIP'=>true,'FORMAT'=>'JSON','ECHO'=>true));必修></p>
?
<h2>GET Location</h2>
?
<p>'ECHO'=>true</p>
?
<p><必修phpnewIP(array('ECHO'=>true));必修></p>
?
<p>'FORMAT'=>'JSON','ECHO'=>true</p>
?
<p><必修php newIP(array('FORMAT'=>'JSON','ECHO'=>true));必修></p>
</body>
</html>
歡迎參與《PHP編程:PHP中實現(xiàn)獲取IP和地理位置類分享》討論,分享您的想法,維易PHP學(xué)院為您提供專業(yè)教程。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/12425.html