十個超有用的php程式碼片段
[PHP]代碼
- 1. 傳送簡訊
- 呼叫 TextMagic API。
-
- // 包含TextMagic PHP 函式庫
- require('textmagic-sms-api-php/TextMagicAPI.php');
-
- // 設定使用者名稱和密碼資訊
- $username = 'myusername';
- $password = 'mypassword';
-
- // 建立一個新的TM 實例
- $router = new TextMagicAPI(array(
- 'username' => ; $ username,
- 'password' => $password
- ));
-
- //發送簡訊到'999-123-4567'
- $result = $router- >send('醒醒醒吧!來! 依照IP找尋位址
-
- function detector_city($ip) {
-
- $default = 'UNKNOWN';
-
- if (!is_string($ip) || strlen($ip ) ) $ip = '8.8.8.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)';
-
- $url = 'http://ipinfodb .com/ip_locator.php?ip=' 。 urlencode($ip);
- $ch = curl_init();
-
- $curl_opt = array(
- CURLOPT_FOLLOWLOCATION => 1,
- CURLOPT_HEADER => 0,CURLOPT_USERAGENT => $curlopt_useragent,
- CURLOPT_URL => $url,
- CURLOPT_TIMEOUT => 'http://' . 'HTTP_HOST'],
- ); $ch, $curl_opt);
-
- $content = curl_exec($ch);
-
- if (!is_null ($curl_info)) {
- $curl_info = curl_getinfo($ch);
- }
-
- curl_close($ch);
-
- if ( preg_match('{
- ;城市: ([^}i', $ content, $regs) ) {
- $city = $regs[1];
- }
- if ( preg_match( '{
- 州/省: ([^ }i', $content, $regs) ) {
- $state = $regs[1];
- }
-
- if( $city!='' && $state!='' ) {
- $location = $city . ','。 $state;
- 回傳$位置;
- }其他{
- 回傳$default;
- }
-
- }
-
- 3. 顯示網頁的原始碼
-
-
$lines = file('http://google.com/');
- foreach ($lines as $line_num => $line) {
- //循環遍歷每一行並新增行號
- echo "Line #{$line_num} : " . htmlspecialchars($line) 。 “
n”;
- }
-
- 4. 檢查伺服器是否使用HTTPS
-
- if ($_SERVER['HTTPS'] != "on") {
- echo "This is not HTTPS";
- }else{
- echo "This是HTTPS";
- }
-
- 5. 顯示Faceboo**絲數
-
- function fb_count( $facebook_name){
- // 範例: https://graph.facebook.com/digimantra
- $data = json_decode(file_get_contents("https: //graph.facebook.com/".$facebook_name));
- echo $data->likes;
- }
-
- 6. 偵測圖片的主要顏色
-
- $i = imagecreatefromjpeg("image.jpg");
-
- for ($x=0;$xfor ($y=0;$y$rgb = imagecolorat($i,$x,$y);
- $r = ($rgb > ;> 16) & 0xFF;
- $g = ($rgb >> & 0xFF;
- $b = $ rgb & 0xFF;
-
- $rTotal = $r;
- $gTotal = $g;
- $bTotal = $b;
- $total ;
- }
- }
- }
- $total ;
- }
- }
- }
- }
-
- $rAverage = round($rTotal/$total);
- $gAverage = round($gTotal/$total);
- $bAverage = round($bTotal/$total);
-
- 7. 取得記憶體資訊使用
-
- echo"Initial: " .memory_get_usage()." bytes n";
- /* prints
- Initial: 361400 bytes
- // http://www.baoluowanfang.com/
- // 讓我們用增加一些記憶體
- for ($i = 0; $i $array []= md5($i);
- }
-
- // 讓我們刪除陣列的一半
- for ($i = 0; $i unset($array [$i]);
- }
-
- echo"Final: ".memory_get_usage()." bytes n";
- /* prints
- Final: 885912 bytes
- */
-
- /* 列印
- 峰值:13687072 位元組
- */
-
- 8. 使用gzcompress() 來壓縮資料。 🎜> 明智的,醫生沒有價格,
- 之前沒有 ultricies elit。
- sit adipiscing elit 但是對不起,對不起,對不起,恐怕Lacinia eu nola 飲料
- id Euismod urna 成員";
-
- $compressed = gzcompress($string);
-
- echo "原始大小:"。strlen($string)."n";
- /* 列印
- 原始大小: 800
- */
-
- echo "壓縮後的大小: ". strlen( $compressed)."n";
- /* 列印
- 壓縮大小: 418
- */
-
- //取回
- $original = gzuncompress($compressed) ;
-
- 9. 使用PHP 做Whois手機
-
- function whois_query($domain) {
-
- //修復網域:
- $domain = strtolower(trim($domain));
- $域= preg_replace('/^http:///i', '', $domain);
- $domain = preg_replace('/^www./i', '', $domain);
- $ domain =explode('/', $domain);
- $domain = trim($domain[0]);
-
- //從網域分割TLD
- $_domain =explode(' .', $domain);
- $lst = count($_domain)-1;
- $ext = $_domain[$lst];
-
- // 您尋找資源和清單
- // 就像維基百科上的這些:
- //
- // http://de.wikipedia.org/wiki/Whois
- //
- $servers = array(
- "biz" = > "whois.neulevel.biz",
- "whois.internic.net",
- "whois.nic.us",
- "coop" =>"whois.nic .coop",
- "whois.nic.info",
- "whois.nic.name",
- "net " => "whois.internic.net",
- "gov" => "whois. nic.gov",
- "edu" => "whois.internic.net",
- " mil" => "rs.internic.net",
- "int" => "whois .iana.org",
- "ac" => "whois.nic.ac",
- "ae" => "whois.uaenic.ae",
- "at" => " whois.ripe .net",
- "au" =>; "whois.aunic.net",
- "be" =>; "whois.dns.be",
- "bg" =>; "whois. ripe.net",
- "br" =>; "whois.registro.br",
- "bz" =>; "whois.belizenic.bz",
- "ca" =>; "whois .cira.ca",
- "cc" =>; "whois.nic.cc",
- "ch" =>; "whois.nic.ch",
- "cl" =>; " whois.nic.cl",
- "cn" =>; "whois.cnnic.net.cn",
- "cz" =>; "whois.nic.cz",
- "de" = >; "whois.nic.de",
- "fr" =>; "whois.nic.fr",
- "hu" =>; "whois.nic.hu",
- "ie" =>; "whois.domainregistry.ie",
- "il" =>; "whois.isoc.org.il",
- "在"=>; "whois.ncst.ernet.in",
- "ir" =>; "whois.nic.ir",
- "mc" =>; "whois.ripe.net",
- "至" => "whois.tonic.to",
- "tv" =>; "whois.tv",
- "ru" =>; "whois.ripn.net",
- "org" =>; "whois.pir.org",
- "aero" =>; "whois.information.aero",
- "nl" =>; "whois.domain-registry.nl"
- );
-
- if (!isset($ servers[$ext])){
- die('錯誤:找不到符合的網卡伺服器! ') ;
- }
-
- $nic_server = $servers[$ext];
-
- $output = '';
-
- // 連接到whois 伺服器:
- if ($conn = fsockopen ($nic_server, 43)) {
- fputs($conn, $domain."rn");
- while(!feof($conn)) {
- $output . = fgets ($conn,128);
- }
- fclose($conn);
- }
- else { die('錯誤: 無法連線到' . $nic_server . '!'); }
-
- 回傳$output;
- }
-
- 10。 透過Email傳送PHP錯誤
-
- //我們自訂的錯誤處理程序
- function nettuts_error_handler($number, $message, $file, $line, $vars) {
- $email = "
-
$line 行和檔案$file 中發生錯誤($number)。強>
- $訊息";
-
- $email .= "
" 。 print_r($vars, 1) 。 " 登入後複製 ";
-
- $headers = '內容類型:text/html;字元集=iso-8859-1' 。 "rn";
-
- // 透過電子郵件將錯誤發送給某人...
- error_log($email, 1, 'you@youremail.com', $headers);
-
- //確保您決定如何回應錯誤(在使用者方面)
- // 要麼回顯錯誤訊息,要麼終止整個專案。由你決定...
- // 下面的程式碼確保我們只在錯誤超過
- // 只是一個通知時「死亡」。
- if ( ($number !== E_NOTICE) && ($number die("出現錯誤,請稍後再試。");
- }
- }
-
- // 我們應該使用我們的自訂函數來處理錯誤。
- set_error_handler('nettuts_error_handler');
-
- // 觸發錯誤...(var 不存在)
- echo$somevarthatdoesnotexist;
複製碼🎜>
|
PHP,PHP