日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
十個(gè)超級(jí)有用的PHP代碼片段

1. 發(fā)送短信

調(diào)用 TextMagic API。

 
 
 
  1. // Include the TextMagic PHP lib  
  2. require('textmagic-sms-api-php/TextMagicAPI.php');  
  3.  
  4. // Set the username and password information  
  5. $username = 'myusername';  
  6. $password = 'mypassword';  
  7.  
  8. // Create a new instance of TM  
  9. $router = new TextMagicAPI(array(  
  10.     'username' => $username,  
  11.     'password' => $password 
  12. ));  
  13.  
  14. // Send a text message to '999-123-4567'  
  15. $result = $router->send('Wake up!', array(9991234567), true);  
  16.  
  17. // result:  Result is: Array ( [messages] => Array ( [19896128] => 9991234567 ) [sent_text] => Wake up! [parts_count] => 1 ) 

2. 根據(jù)IP查找地址

 
 
 
  1. function detect_city($ip) {  
  2.  
  3.         $default = 'UNKNOWN';  
  4.  
  5.         if (!is_string($ip) || strlen($ip) < 1 || $ip == '127.0.0.1' || $ip == 'localhost')  
  6.             $ip = '8.8.8.8';  
  7.  
  8.         $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)';  
  9.  
  10.         $url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode($ip);  
  11.         $ch = curl_init();  
  12.  
  13.         $curl_opt = array(  
  14.             CURLOPT_FOLLOWLOCATION  => 1,  
  15.             CURLOPT_HEADER      => 0,  
  16.             CURLOPT_RETURNTRANSFER  => 1,  
  17.             CURLOPT_USERAGENT   => $curlopt_useragent,  
  18.             CURLOPT_URL       => $url,  
  19.             CURLOPT_TIMEOUT         => 1,  
  20.             CURLOPT_REFERER         => 'http://' . $_SERVER['HTTP_HOST'],  
  21.         );  
  22.  
  23.         curl_setopt_array($ch, $curl_opt);  
  24.  
  25.         $content = curl_exec($ch);  
  26.  
  27.         if (!is_null($curl_info)) {  
  28.             $curl_info = curl_getinfo($ch);  
  29.         }  
  30.  
  31.         curl_close($ch);  
  32.  
  33.         if ( preg_match('{
  34. City : ([^<]*)
  35. }i', $content, $regs) )  {  
  36.             $city = $regs[1];  
  37.         }  
  38.         if ( preg_match('{
  39. State/Province : ([^<]*)
  40. }i', $content, $regs) )  {  
  41.             $state = $regs[1];  
  42.         }  
  43.  
  44.         if( $city!='' && $state!='' ){  
  45.           $location = $city . ', ' . $state;  
  46.           return $location;  
  47.         }else{  
  48.           return $default;  
  49.         }  
  50.  
  51.     } 

3. 顯示網(wǎng)頁的源代碼

 
 
 
  1. $lines = file('http://google.com/');  
  2. foreach ($lines as $line_num => $line) {  
  3.     // loop thru each line and prepend line numbers  
  4.     echo "Line #{$line_num} : " . htmlspecialchars($line) . "
    \n";  

4. 檢查服務(wù)器是否使用HTTPS

 
 
 
  1. if ($_SERVER['HTTPS'] != "on") {  
  2.     echo "This is not HTTPS";  
  3. }else{  
  4.     echo "This is HTTPS";  

5. 顯示Facebook粉絲數(shù)量

 
 
 
  1. function fb_fan_count($facebook_name){  
  2.     // Example: https://graph.facebook.com/digimantra  
  3.     $data = json_decode(file_get_contents("https://graph.facebook.com/".$facebook_name));  
  4.     echo $data->likes;  

6. 檢測(cè)圖片的主要顏色

 
 
 
  1. $i = imagecreatefromjpeg("image.jpg");  
  2.  
  3. for ($x=0;$x
  4.     for ($y=0;$y
  5.         $rgb = imagecolorat($i,$x,$y);  
  6.         $r   = ($rgb >> 16) & 0xFF;  
  7.         $g   = ($rgb >>  & 0xFF;  
  8.         $b   = $rgb & 0xFF;  
  9.  
  10.         $rTotal += $r;  
  11.         $gTotal += $g;  
  12.         $bTotal += $b;  
  13.         $total++;  
  14.     }  
  15. }  
  16.  
  17. $rAverage = round($rTotal/$total);  
  18. $gAverage = round($gTotal/$total);  
  19. $bAverage = round($bTotal/$total); 

7. 獲取內(nèi)存使用信息

 
 
 
  1. echo "Initial: ".memory_get_usage()." bytes \n";  
  2. /* prints  
  3. Initial: 361400 bytes  
  4. */ 
  5.  
  6. // let's use up some memory  
  7. for ($i = 0; $i < 100000; $i++) {  
  8.     $array []= md5($i);  
  9. }  
  10.  
  11. // let's remove half of the array  
  12. for ($i = 0; $i < 100000; $i++) {  
  13.     unset($array[$i]);  
  14. }  
  15.  
  16. echo "Final: ".memory_get_usage()." bytes \n";  
  17. /* prints  
  18. Final: 885912 bytes  
  19. */ 
  20.  
  21. echo "Peak: ".memory_get_peak_usage()." bytes \n";  
  22. /* prints  
  23. Peak: 13687072 bytes  
  24. */ 

8. 使用 gzcompress() 壓縮數(shù)據(jù)

 
 
 
  1. $string =  
  2. "Lorem ipsum dolor sit amet, consectetur  
  3. adipiscing elit. Nunc ut elit id mi ultricies  
  4. adipiscing. Nulla facilisi. Praesent pulvinar,  
  5. sapien vel feugiat vestibulum, nulla dui pretium orci,  
  6. non ultricies elit lacus quis ante. Lorem ipsum dolor  
  7. sit amet, consectetur adipiscing elit. Aliquam  
  8. pretium ullamcorper urna quis iaculis. Etiam ac massa  
  9. sed turpis tempor luctus. Curabitur sed nibh eu elit  
  10. mollis congue. Praesent ipsum diam, consectetur vitae  
  11. ornare a, aliquam a nunc. In id magna pellentesque  
  12. tellus posuere adipiscing. Sed non mi metus, at lacinia  
  13. augue. Sed magna nisi, ornare in mollis in, mollis  
  14. sed nunc. Etiam at justo in leo congue mollis.  
  15. Nullam in neque eget metus hendrerit scelerisque  
  16. eu non enim. Ut malesuada lacus eu nulla bibendum  
  17. id euismod urna sodales. ";  
  18.  
  19. $compressed = gzcompress($string);  
  20.  
  21. echo "Original size: ". strlen($string)."\n";  
  22. /* prints  
  23. Original size: 800  
  24. */  
  25.  
  26. echo "Compressed size: ". strlen($compressed)."\n";  
  27. /* prints  
  28. Compressed size: 418  
  29. */  
  30.  
  31. // getting it back  
  32. $original = gzuncompress($compressed); 

9. 使用PHP做Whois檢查

 
 
 
  1. function whois_query($domain) {  
  2.  
  3.     // fix the domain name:  
  4.     $domain = strtolower(trim($domain));  
  5.     $domain = preg_replace('/^http:\/\//i', '', $domain);  
  6.     $domain = preg_replace('/^www\./i', '', $domain);  
  7.     $domain = explode('/', $domain);  
  8.     $domain = trim($domain[0]);  
  9.  
  10.     // split the TLD from domain name  
  11.     $_domain = explode('.', $domain);  
  12.     $lst = count($_domain)-1;  
  13.     $ext = $_domain[$lst];  
  14.  
  15.     // You find resources and lists  
  16.     // like these on wikipedia:  
  17.     //  
  18.     // http://de.wikipedia.org/wiki/Whois  
  19.     //  
  20.     $servers = array(  
  21.         "biz" => "whois.neulevel.biz",  
  22.         "com" => "whois.internic.net",  
  23.         "us" => "whois.nic.us",  
  24.         "coop" => "whois.nic.coop",  
  25.         "info" => "whois.nic.info",  
  26.         "name" => "whois.nic.name",  
  27.         "net" => "whois.internic.net",  
  28.         "gov" => "whois.nic.gov",  
  29.         "edu" => "whois.internic.net",  
  30.         "mil" => "rs.internic.net",  
  31.         "int" => "whois.iana.org",  
  32.         "ac" => "whois.nic.ac",  
  33.         "ae" => "whois.uaenic.ae",  
  34.         "at" => "whois.ripe.net",  
  35.         "au" => "whois.aunic.net",  
  36.         "be" => "whois.dns.be",  
  37.         "bg" => "whois.ripe.net",  
  38.         "br" => "whois.registro.br",  
  39.         "bz" => "whois.belizenic.bz",  
  40.         "ca" => "whois.cira.ca",  
  41.         "cc" => "whois.nic.cc",  
  42.         "ch" => "whois.nic.ch",  
  43.         "cl" => "whois.nic.cl",  
  44.         "cn" => "whois.cnnic.net.cn",  
  45.         "cz" => "whois.nic.cz",  
  46.         "de" => "whois.nic.de",  
  47.         "fr" => "whois.nic.fr",  
  48.         "hu" => "whois.nic.hu",  
  49.         "ie" => "whois.domainregistry.ie",  
  50.         "il" => "whois.isoc.org.il",  
  51.         "in" => "whois.ncst.ernet.in",  
  52.         "ir" => "whois.nic.ir",  
  53.         "mc" => "whois.ripe.net",  
  54.         "to" => "whois.tonic.to",  
  55.         "tv" => "whois.tv",  
  56.         "ru" => "whois.ripn.net",  
  57.         "org" => "whois.pir.org",  
  58.         "aero" => "whois.information.aero",  
  59.         "nl" => "whois.domain-registry.nl"  
  60.     );  
  61.  
  62.     if (!isset($servers[$ext])){  
  63.         die('Error: No matching nic server found!');  
  64.     }  
  65.  
  66.     $nic_server = $servers[$ext];  
  67.  
  68.     $output = '';  
  69.  
  70.     // connect to whois server:  
  71.     if ($conn = fsockopen ($nic_server, 43)) {  
  72.         fputs($conn, $domain."\r\n");  
  73.         while(!feof($conn)) {  
  74.             $output .= fgets($conn,128);  
  75.         }  
  76.         fclose($conn);  
  77.     }  
  78.     else { die('Error: Could not connect to ' . $nic_server . '!'); }  
  79.  
  80.     return $output;  

10. 通過Email發(fā)送PHP錯(cuò)誤

 
 
 
  1.  
  2. // Our custom error handler  
  3. function nettuts_error_handler($number, $message, $file, $line, $vars){  
  4.     $email = "  
  5.         

    An error ($number) occurred on line  

  6.         $line and in the file: $file.  
  7.         

     $message 

    ";  
  8.  
  9.     $email .= "
    " . print_r($vars, 1) . "
    ";  
  10.  
  11.     $headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  
  12.  
  13.     // Email the error to someone...  
  14.     error_log($email, 1, 'you@youremail.com', $headers);  
  15.  
  16.     // Make sure that you decide how to respond to errors (on the user's side)  
  17.     // Either echo an error message, or kill the entire project. Up to you...  
  18.     // The code below ensures that we only "die" if the error was more than  
  19.     // just a NOTICE.  
  20.     if ( ($number !== E_NOTICE) && ($number < 2048) ) {  
  21.         die("There was an error. Please try again later.");  
  22.     }  
  23. }  
  24.  
  25. // We should use our custom function to handle errors.  
  26. set_error_handler('nettuts_error_handler');  
  27.  
  28. // Trigger an error... (var doesn't exist)  
  29. echo $somevarthatdoesnotexist; 

原文:http://www.oschina.net/question/28_36708

【編輯推薦】

  1. 2011年最熱門的開源PHP項(xiàng)目回顧
  2. PHP 5.4將被包含進(jìn)Fedora 17
  3. PHP 7:PHP變量和常量的定義
  4. 大話PHP之性能
  5. 使用PHP腳本來寫Daemon程序

本文題目:十個(gè)超級(jí)有用的PHP代碼片段
文章起源:http://www.5511xx.com/article/cochehj.html