又十个超级有用的PHP代码片段
好东西要大家一起分享,上次分享了十个,这次再来十个超级有用的PHP代码片段。1. 发送短信调用 TextMagic API。 // Set the username and password information
$username = 'myusername'; $password = 'mypassword'; // Create a new instance of TM // Send a text message to '999-123-4567' // result: Result is: Array ( [messages] => Array ( [19896128] => 9991234567 ) [sent_text] => Wake up! [parts_count] => 1 ) 2. 根据IP查找地址$default = 'UNKNOWN';
if (!is_string($ip) || strlen($ip) < 1 || $ip == '127.0.0.1' || $ip == 'localhost') $curlopt_useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)'; $url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode($ip); $curl_opt = array( curl_setopt_array($ch,$curl_opt); $content = curl_exec($ch); if (!is_null($curl_info)) { curl_close($ch); if ( preg_match('{ $city = $regs[1]; } if ( preg_match('{ $state = $regs[1]; } if( $city!='' && $state!='' ){ } 3. 显示网页的源代码 $line) {
// loop thru each line and prepend line numbers
echo "Line #{$line_num} : " . htmlspecialchars($line) . " 4. 检查服务器是否使用HTTPS
5. 显示Facebook粉丝数量likes;
}
6. 检测图片的主要颜色for ($x=0;$x<imagesx($i);$x++) {
for ($y=0;$y<imagesy($i);$y++) { $rgb = imagecolorat($i,$x,$y); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> & 0xFF; $b = $rgb & 0xFF; $rTotal += $r; $rAverage = round($rTotal/$total); 7. 获取内存使用信息// let's use up some memory
for ($i = 0; $i < 100000; $i++) { $array []= md5($i); } // let's remove half of the array echo "Final: ".memory_get_usage()." bytes n"; echo "Peak: ".memory_get_peak_usage()." bytes n"; 8. 使用 gzcompress() 压缩数据$compressed = gzcompress($string);
echo "Original size: ". strlen($string)."n"; echo "Compressed size: ". strlen($compressed)."n"; // getting it back 9. 使用PHP做Whois检查// fix the domain name:
$domain = strtolower(trim($domain)); $domain = preg_replace('/^http:///i','',$domain); $domain = preg_replace('/^www./i',$domain); $domain = explode('/',$domain); $domain = trim($domain[0]); // split the TLD from domain name // You find resources and lists if (!isset($servers[$ext])){ $nic_server = $servers[$ext]; $output = ''; // connect to whois server: return $output;
10. 通过Email发送PHP错误// Our custom error handler
function nettuts_error_handler($number,$message,$file,$line,$vars){ $email = " An error ($number) occurred on line $lineand in thefile: $file.$message ";$email .= " " . print_r($vars,1) . ""; $headers = 'Content-type: text/html; charset=iso-8859-1' . "rn"; // Email the error to someone... // Make sure that you decide how to respond to errors (on the user's side) // We should use our custom function to handle errors. // Trigger an error... (var doesn't exist) 是不是也很精彩,和一起收藏吧 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |