非常に便利な 10 個の PHP コード スニペット

伊谢尔伦
リリース: 2016-11-24 14:27:50
オリジナル
1212 人が閲覧しました

1. SMS を送信する

TextMagic API を呼び出します。

// Include the TextMagic PHP lib  
require('textmagic-sms-api-php/TextMagicAPI.php');  
 
// Set the username and password information  
$username = 'myusername';  
$password = 'mypassword';  
 
// Create a new instance of TM  
$router = new TextMagicAPI(array(  
    'username' => $username,  
    'password' => $password 
));  
 
// Send a text message to '999-123-4567'  
$result = $router->send('Wake up!', array(9991234567), true);  
 
// result:  Result is: Array ( [messages] => Array ( [19896128] => 9991234567 ) [sent_text] => Wake up! [parts_count] => 1 )
ログイン後にコピー

2. IPに基づいてアドレスを検索

function detect_city($ip) {  
 
        $default = 'UNKNOWN';  
 
        if (!is_string($ip) || strlen($ip) < 1 || $ip == &#39;127.0.0.1&#39; || $ip == &#39;localhost&#39;)  
            $ip = &#39;8.8.8.8&#39;;  
 
        $curlopt_useragent = &#39;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)&#39;;  
 
        $url = &#39;http://ipinfodb.com/ip_locator.php?ip=&#39; . urlencode($ip);  
        $ch = curl_init();  
 
        $curl_opt = array(  
            CURLOPT_FOLLOWLOCATION  => 1,  
            CURLOPT_HEADER      => 0,  
            CURLOPT_RETURNTRANSFER  => 1,  
            CURLOPT_USERAGENT   => $curlopt_useragent,  
            CURLOPT_URL       => $url,  
            CURLOPT_TIMEOUT         => 1,  
            CURLOPT_REFERER         => &#39;http://&#39; . $_SERVER[&#39;HTTP_HOST&#39;],  
        );  
 
        curl_setopt_array($ch, $curl_opt);  
 
        $content = curl_exec($ch);  
 
        if (!is_null($curl_info)) {  
            $curl_info = curl_getinfo($ch);  
        }  
 
        curl_close($ch);  
 
        if ( preg_match(&#39;{<li>City : ([^<]*)</li>}i&#39;, $content, $regs) )  {  
            $city = $regs[1];  
        }  
        if ( preg_match(&#39;{<li>State/Province : ([^<]*)</li>}i&#39;, $content, $regs) )  {  
            $state = $regs[1];  
        }  
 
        if( $city!=&#39;&#39; && $state!=&#39;&#39; ){  
          $location = $city . &#39;, &#39; . $state;  
          return $location;  
        }else{  
          return $default;  
        }  
 
    }
ログイン後にコピー

4. サーバーがHTTPSを使用しているかどうかを確認します

<?php // display source code  
$lines = file(&#39;http://google.com/&#39;);  
foreach ($lines as $line_num => $line) {  
    // loop thru each line and prepend line numbers  
    echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br>\n";  
}
ログイン後にコピー

5.

if ($_SERVER[&#39;HTTPS&#39;] != "on") {  
    echo "This is not HTTPS";  
}else{  
    echo "This is HTTPS";  
}
ログイン後にコピー

6. 画像の検出


function fb_fan_count($facebook_name){  
    // Example: https://graph.facebook.com/digimantra  
    $data = json_decode(file_get_contents("https://graph.facebook.com/".$facebook_name));  
    echo $data->likes;  
}
ログイン後にコピー

7. メモリ使用量情報を取得する


$i = imagecreatefromjpeg("image.jpg");  
 
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;  
        $gTotal += $g;  
        $bTotal += $b;  
        $total++;  
    }  
}  
 
$rAverage = round($rTotal/$total);  
$gAverage = round($gTotal/$total);  
$bAverage = round($bTotal/$total);
ログイン後にコピー

8. PHP を使用して Whois チェックを行う


echo "Initial: ".memory_get_usage()." bytes \n";  
/* prints  
Initial: 361400 bytes  
*/ 
 
// let&#39;s use up some memory  
for ($i = 0; $i < 100000; $i++) {  
    $array []= md5($i);  
}  
 
// let&#39;s remove half of the array  
for ($i = 0; $i < 100000; $i++) {  
    unset($array[$i]);  
}  
 
echo "Final: ".memory_get_usage()." bytes \n";  
/* prints  
Final: 885912 bytes  
*/ 
 
echo "Peak: ".memory_get_peak_usage()." bytes \n";  
/* prints  
Peak: 13687072 bytes  
*/
ログイン後にコピー
10 PHP エラーをメールで送信


rree


関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!