【PHP】网络相关封装的函数
生活随笔
收集整理的這篇文章主要介紹了
【PHP】网络相关封装的函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
if (!function_exists('getDnsRecord')) {/*** 封裝 dns_get_record* 默認返回 dnsA記錄** 返回值:* 以分號隔開的多個IP地址* * 說明:* @dns_get_record() 解決報錯: A temporary server error occurred.*/function getDnsRecord($domain, $type = DNS_A){$ipaddr = "0";if (DNS_AAAA == $type) {if (dns_check_record($domain, 'AAAA')) {$dns = @dns_get_record($domain, $type);$ipaddr = empty($dns) ? '0' : implode(';', array_column($dns, 'ipv6'));}} elseif (DNS_A == $type) {if (dns_check_record($domain, 'A')) {$dns = @dns_get_record($domain, $type);$ipaddr = empty($dns) ? '0' : implode(';', array_column($dns, 'ip'));}}return $ipaddr;}
}if (!function_exists('httpGetWebInfo')) {/*** 具體見:https://www.php.net/manual/zh/function.curl-getinfo.php* size_download: 字節 。* speed_download: 字節/秒 ,下載完成后的速度。* total_time:秒,包括域名解析,以及 TCP 連接過程中時間*/function httpGetWebInfo($url, $iptype = 6){$data = [];$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_TIMEOUT, 200);curl_setopt($ch, CURLOPT_HEADER, false); //不需要輸出頭部信息curl_setopt($ch, CURLOPT_NOBODY, false); //輸出內容curl_setopt($ch, CURLOPT_IPRESOLVE, 4 == $iptype ? CURL_IPRESOLVE_V4 : CURL_IPRESOLVE_V6);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //超時重試curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //抓取轉跳curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); //post方式提交curl_exec($ch);if (!curl_errno($ch)) {$data = curl_getinfo($ch);}curl_close($ch);return $data;}
}if (!function_exists("getIPAddrNumByIPv4Mask")) {/*** 根據IPv4掩碼地址獲取子網有多少個地址*/function getIPAddrNumByIPv4Mask($maskAddr){if (strpos($maskAddr, '/')) {list($addr, $maskLen) = explode('/', $maskAddr);$total = pow(2, 32 - intval($maskLen));return (32 == $maskLen) ? $total : $total - 2;}return 0;}
}/* 原理是利用對數求出欲轉換的字節數是 1024 的幾次方* 其實就是利用對數的特性確定單位*/
function size2mb($size,$digits=2){ //digits,要保留幾位小數$unit = array('','K','M','G','T','P'); //單位數組,是必須1024進制$base = 1024; //對數的基數$i = floor(log($size,$base)); //字節數對1024取對數,值向下取整return round($size/pow($base,$i),$digits).' '.$unit[$i] . 'B';
}
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的【PHP】网络相关封装的函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Rsyslog】 从json 中通过正
- 下一篇: 【PHP】curl_init() 如