淘宝公司提供了一个很好用的IP地理信息查询接口。 在这里:http://ip.taobao.com/ TaobaoIPQuery2这个类将极大的简化相关的信息查询。 类 TaobaoIPQuery2 文件: 1 ? php 2 /* Usage: 3 * $IPInfo = TaobaoIPQuery2::getIPInfo('IPAddress'); 4 5 http://www.
淘宝公司提供了一个很好用的IP地理信息查询接口。
在这里:http://ip.taobao.com/
TaobaoIPQuery2这个类将极大的简化相关的信息查询。
类 TaobaoIPQuery2 文件:
<span> 1</span> <span>php </span><span> 2</span> <span>/*</span><span> Usage: </span><span> 3</span> <span> * $IPInfo = TaobaoIPQuery2::getIPInfo('IPAddress'); </span><span> 4</span> <span> 5</span> <span>http://www.cnblogs.com/roucheng/ </span><span> 6</span> <span>*/</span> <span> 7</span> <span>Class</span><span> TaobaoIPQuery2{ </span><span> 8</span> <span>private</span> <span>static</span> <span>$_requestURL</span> = 'http://ip.taobao.com/service/getIpInfo.php'<span>; </span><span> 9</span> <span>public</span> <span>static</span> <span>function</span> getIPInfo(<span>$ip</span><span>){ </span><span>10</span> <span>$long</span> = <span>ip2long</span>(<span>$ip</span><span>); </span><span>11</span> <span>if</span>(<span>$long</span> === 0<span>){ </span><span>12</span> <span>throw</span> <span>new</span> <span>Exception</span>('IP address error', 5<span>); </span><span>13</span> <span> } </span><span>14</span> <span>$ip</span>=<span>long2ip</span>(<span>$long</span><span>); </span><span>15</span> <span>$IPInfo</span> = self::queryIPInfo(<span>$ip</span><span>); </span><span>16</span> <span>return</span> self::parseJSON(<span>$IPInfo</span><span>); </span><span>17</span> <span> } </span><span>18</span> <span>19</span> <span>private</span> <span>static</span> <span>function</span> queryIPInfo(<span>$ip</span><span>){ </span><span>20</span> <span>$query</span> = <span>http_build_query</span>(<span>array</span>('ip'=><span>$ip</span><span>)); </span><span>21</span> <span>$ch</span> =<span> curl_init(); </span><span>22</span> <span>$options</span> = <span>array</span><span>( </span><span>23</span> CURLOPT_URL => <span>sprintf</span>('%s?%s', self::<span>$_requestURL</span>, <span>$query</span>), <span>24</span> CURLOPT_RETURNTRANSFER => <span>true</span>, <span>25</span> CURLOPT_AUTOREFERER => <span>false</span>, <span>26</span> CURLOPT_FOLLOWLOCATION => <span>false</span>, <span>27</span> CURLOPT_HEADER => <span>false</span>, <span>28</span> CURLOPT_TIMEOUT => 3.0, <span>29</span> <span> ); </span><span>30</span> curl_setopt_array(<span>$ch</span>, <span>$options</span><span>); </span><span>31</span> <span>$content</span> = curl_exec(<span>$ch</span><span>); </span><span>32</span> curl_close(<span>$ch</span><span>); </span><span>33</span> <span>return</span> <span>$content</span><span>; </span><span>34</span> <span> } </span><span>35</span> <span>36</span> <span>private</span> <span>static</span> <span>function</span> parseJSON(<span>$json</span><span>){ </span><span>37</span> <span>$O</span> = json_decode (<span>$json</span>, <span>true</span><span>); </span><span>38</span> <span>if</span>(<span>false</span> === <span>is_null</span>(<span>$O</span><span>)){ </span><span>39</span> <span>return</span> <span>$O</span><span>; </span><span>40</span> <span> } </span><span>41</span> <span>if</span> (<span>version_compare</span>(<span>PHP_VERSION</span>, '5.3.0', '>='<span>)) { </span><span>42</span> <span>$errorCode</span> =<span> json_last_error(); </span><span>43</span> <span>if</span>(<span>isset</span>(self::<span>$_JSONParseError</span>[<span>$errorCode</span><span>])){ </span><span>44</span> <span>throw</span> <span>new</span> <span>Exception</span>(self::<span>$_JSONParseError</span>[<span>$errorCode</span>], 5<span>); </span><span>45</span> <span> } </span><span>46</span> <span> } </span><span>47</span> <span>throw</span> <span>new</span> <span>Exception</span>('JSON parse error', 5<span>); </span><span>48</span> <span> } </span><span>49</span> <span>50</span> <span>private</span> <span>static</span> <span>$_JSONParseError</span> = <span>array</span><span>( </span><span>51</span> JSON_ERROR_NONE=>'No error has occurred', <span>52</span> JSON_ERROR_DEPTH=>'The maximum stack depth has been exceeded', <span>53</span> JSON_ERROR_CTRL_CHAR=>'Control character error, possibly incorrectly encoded', <span>54</span> JSON_ERROR_STATE_MISMATCH=>'Invalid or malformed JSON', <span>55</span> JSON_ERROR_SYNTAX=>'Syntax error', <span>56</span> JSON_ERROR_UTF8=>'Malformed UTF-8 characters, possibly incorrectly encoded', <span>57</span> <span> ); </span><span>58</span> }
TaobaoIPQuery2.Class.php:
<span> 1</span> <span>php </span><span> 2</span> <span>Class</span><span> TaobaoIPQuery2{ </span><span> 3</span> <span>private</span> <span>static</span> <span>$_requestURL</span> = 'http://ip.taobao.com/service/getIpInfo.php'<span>; </span><span> 4</span> <span>public</span> <span>static</span> <span>function</span> getIPInfo(<span>$ip</span><span>){ </span><span> 5</span> <span>$long</span> = <span>ip2long</span>(<span>$ip</span><span>); </span><span> 6</span> <span>if</span>(<span>$long</span> === 0<span>){ </span><span> 7</span> <span>throw</span> <span>new</span> <span>Exception</span>('IP address error', 5<span>); </span><span> 8</span> <span> } </span><span> 9</span> <span>$ip</span>=<span>long2ip</span>(<span>$long</span><span>); </span><span>10</span> <span>$IPInfo</span> = self::queryIPInfo(<span>$ip</span><span>); </span><span>11</span> <span>return</span> self::parseJSON(<span>$IPInfo</span><span>); </span><span>12</span> <span> } </span><span>13</span> <span>14</span> <span>private</span> <span>static</span> <span>function</span> queryIPInfo(<span>$ip</span><span>){ </span><span>15</span> <span>$query</span> = <span>http_build_query</span>(<span>array</span>('ip'=><span>$ip</span><span>)); </span><span>16</span> <span>$ch</span> =<span> curl_init(); </span><span>17</span> <span>$options</span> = <span>array</span><span>( </span><span>18</span> CURLOPT_URL => <span>sprintf</span>('%s?%s', self::<span>$_requestURL</span>, <span>$query</span>), <span>19</span> CURLOPT_RETURNTRANSFER => <span>true</span>, <span>20</span> CURLOPT_AUTOREFERER => <span>false</span>, <span>21</span> CURLOPT_FOLLOWLOCATION => <span>false</span>, <span>22</span> CURLOPT_HEADER => <span>false</span>, <span>23</span> CURLOPT_TIMEOUT => 3.0, <span>24</span> <span> ); </span><span>25</span> curl_setopt_array(<span>$ch</span>, <span>$options</span><span>); </span><span>26</span> <span>$content</span> = curl_exec(<span>$ch</span><span>); </span><span>27</span> curl_close(<span>$ch</span><span>); </span><span>28</span> <span>return</span> <span>$content</span><span>; </span><span>29</span> <span> } </span><span>30</span> <span>31</span> <span>private</span> <span>static</span> <span>function</span> parseJSON(<span>$json</span><span>){ </span><span>32</span> <span>$O</span> = json_decode (<span>$json</span>, <span>true</span><span>); </span><span>33</span> <span>if</span>(<span>false</span> === <span>is_null</span>(<span>$O</span><span>)){ </span><span>34</span> <span>return</span> <span>$O</span><span>; </span><span>35</span> <span> } </span><span>36</span> <span>if</span> (<span>version_compare</span>(<span>PHP_VERSION</span>, '5.3.0', '>='<span>)) { </span><span>37</span> <span>$errorCode</span> =<span> json_last_error(); </span><span>38</span> <span>if</span>(<span>isset</span>(self::<span>$_JSONParseError</span>[<span>$errorCode</span><span>])){ </span><span>39</span> <span>throw</span> <span>new</span> <span>Exception</span>(self::<span>$_JSONParseError</span>[<span>$errorCode</span>], 5<span>); </span><span>40</span> <span> } </span><span>41</span> <span> } </span><span>42</span> <span>throw</span> <span>new</span> <span>Exception</span>('JSON parse error', 5<span>); </span><span>43</span> <span> } </span><span>44</span> <span>/*</span><span> http://www.cnblogs.com/roucheng/ </span><span>*/</span> <span>45</span> <span>private</span> <span>static</span> <span>$_JSONParseError</span> = <span>array</span><span>( </span><span>46</span> JSON_ERROR_NONE=>'No error has occurred', <span>47</span> JSON_ERROR_DEPTH=>'The maximum stack depth has been exceeded', <span>48</span> JSON_ERROR_CTRL_CHAR=>'Control character error, possibly incorrectly encoded', <span>49</span> JSON_ERROR_STATE_MISMATCH=>'Invalid or malformed JSON', <span>50</span> JSON_ERROR_SYNTAX=>'Syntax error', <span>51</span> JSON_ERROR_UTF8=>'Malformed UTF-8 characters, possibly incorrectly encoded', <span>52</span> <span> ); </span><span>53</span> }
调用:
<span>$ip</span> = <span>$_SERVER</span>["REMOTE_ADDR"<span>]; </span><span>$ipquery</span> = <span>new</span> taobaoIPQuery(<span>$ip</span><span>); </span><span>$region</span> = <span>$ipquery</span>-><span>get_region(); </span><span>$country</span> = <span>$ipquery</span>-><span>get_country(); </span><span>$city</span> = <span>$ipquery</span>->get_city();
另外新浪也提供接口:http://ipapi.sinaapp.com/

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

WebStorm Mac版
好用的JavaScript開發工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中