|
1. 发送短信
调用 TextMagic API。
01 | // Include the TextMagic PHP lib |
02 | require('textmagic-sms-api-php/TextMagicAPI.php'); |
04 | // Set the username and password information |
05 | $username = 'myusername'; |
06 | $password = 'mypassword'; |
08 | // Create a new instance of TM |
09 | $router = new TextMagicAPI(array( |
10 | 'username'=> $username, |
14 | // Send a text message to '999-123-4567' |
15 | $result = $router->send('Wake up!',array(9991234567), true); |
17 | // result: Result is: Array ( [messages] => Array ( [19896128] => 9991234567 ) [sent_text] => Wake up! [parts_count] => 1 ) |
2. 根据IP查找地址
01 | function detect_city($ip) { |
05 | if(!is_string($ip) ||strlen($ip) < 1 ||$ip == '127.0.0.1'|| $ip == 'localhost') |
08 | $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)'; |
10 | $url= 'http://ipinfodb.com/ip_locator.php?ip='. urlencode($ip); |
14 | CURLOPT_FOLLOWLOCATION => 1, |
16 | CURLOPT_RETURNTRANSFER => 1, |
17 | CURLOPT_USERAGENT =>$curlopt_useragent, |
20 | CURLOPT_REFERER =>'http://' . $_SERVER['HTTP_HOST'], |
23 | curl_setopt_array($ch,$curl_opt); |
25 | $content= curl_exec($ch); |
27 | if(!is_null($curl_info)) { |
28 | $curl_info= curl_getinfo($ch); |
33 | if( preg_match('{<li>City : ([^<]*)</li>}i',$content, $regs) ) { |
36 | if( preg_match('{<li>State/Province : ([^<]*)</li>}i',$content, $regs) ) { |
40 | if($city!=''&& $state!=''){ |
41 | $location= $city . ', ' . $state; |
3. 显示网页的源代码
1 | <?php // display source code |
2 | $lines = file('http://google.com/'); |
3 | foreach ($lines as$line_num => $line) { |
4 | // loop thru each line and prepend line numbers |
5 | echo"Line #<b>{$line_num}</b> : " . htmlspecialchars($line) ."<br>\n"; |
4. 检查服务器是否使用HTTPS
1 | if ($_SERVER['HTTPS'] !="on") { |
2 | echo"This is not HTTPS"; |
5. 显示Faceboo丝数量
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)); |
6. 检测图片的主要颜色
01 | $i = imagecreatefromjpeg("image.jpg"); |
03 | for ($x=0;$x<imagesx($i);$x++){ |
04 | for($y=0;$y<imagesy($i);$y++){ |
05 | $rgb= imagecolorat($i,$x,$y); |
06 | $r= ($rgb >> 16) & 0xFF; |
17 | $rAverage = round($rTotal/$total); |
18 | $gAverage = round($gTotal/$total); |
19 | $bAverage = round($bTotal/$total); |
7. 获取内存使用信息
01 | echo "Initial: ".memory_get_usage()." bytes \n"; |
06 | // let's use up some memory |
07 | for ($i = 0; $i < 100000; $i++) { |
11 | // let's remove half of the array |
12 | for ($i = 0; $i < 100000; $i++) { |
16 | echo "Final: ".memory_get_usage()." bytes \n"; |
21 | echo "Peak: ".memory_get_peak_usage()." bytes \n"; |
8. 使用 gzcompress() 压缩数据
02 | "Lorem ipsum dolor sit amet, consectetur |
03 | adipiscing elit. Nunc ut elit id mi ultricies |
04 | adipiscing. Nulla facilisi. Praesent pulvinar, |
05 | sapien vel feugiat vestibulum, nulla dui pretium orci, |
06 | non ultricies elit lacus quis ante. Lorem ipsum dolor |
07 | sit amet, consectetur adipiscing elit. Aliquam |
08 | pretium ullamcorper urna quis iaculis. Etiam ac massa |
09 | 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. "; |
19 | $compressed = gzcompress($string); |
21 | echo "Original size: ". strlen($string)."\n"; |
26 | echo "Compressed size: ". strlen($compressed)."\n"; |
32 | $original = gzuncompress($compressed); |
9. 使用PHP做Whois检查
01 | function whois_query($domain) { |
03 | // fix the domain name: |
04 | $domain= strtolower(trim($domain)); |
05 | $domain= preg_replace('/^http:\/\//i','', $domain); |
06 | $domain= preg_replace('/^www\./i','', $domain); |
07 | $domain= explode('/',$domain); |
08 | $domain= trim($domain[0]); |
10 | // split the TLD from domain name |
11 | $_domain= explode('.',$domain); |
12 | $lst= count($_domain)-1; |
15 | // You find resources and lists |
16 | // like these on wikipedia: |
18 | // http://de.wikipedia.org/wiki/Whois |
21 | "biz"=> "whois.neulevel.biz", |
22 | "com"=> "whois.internic.net", |
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", |
33 | "ae"=> "whois.uaenic.ae", |
34 | "at"=> "whois.ripe.net", |
35 | "au"=> "whois.aunic.net", |
37 | "bg"=> "whois.ripe.net", |
38 | "br"=> "whois.registro.br", |
39 | "bz"=> "whois.belizenic.bz", |
40 | "ca"=> "whois.cira.ca", |
44 | "cn"=> "whois.cnnic.net.cn", |
49 | "ie"=> "whois.domainregistry.ie", |
50 | "il"=> "whois.isoc.org.il", |
51 | "in"=> "whois.ncst.ernet.in", |
53 | "mc"=> "whois.ripe.net", |
54 | "to"=> "whois.tonic.to", |
56 | "ru"=> "whois.ripn.net", |
57 | "org"=> "whois.pir.org", |
58 | "aero"=> "whois.information.aero", |
59 | "nl"=> "whois.domain-registry.nl" |
62 | if(!isset($servers[$ext])){ |
63 | die('Error: No matching nic server found!'); |
66 | $nic_server= $servers[$ext]; |
70 | // connect to whois server: |
71 | if($conn = fsockopen ($nic_server, 43)) { |
72 | fputs($conn,$domain."\r\n"); |
74 | $output.= fgets($conn,128); |
78 | else{ die('Error: Could not connect to '. $nic_server . '!'); } |
10. 通过Email发送PHP错误
03 | // Our custom error handler |
04 | function nettuts_error_handler($number,$message, $file, $line,$vars){ |
06 | <p>An error ($number) occurred on line |
07 | <strong>$line</strong>and in the <strong>file:$file.</strong> |
10 | $email.= "<pre>" . print_r($vars, 1) ."</pre>"; |
12 | $headers= 'Content-type: text/html; charset=iso-8859-1'. "\r\n"; |
14 | // Email the error to someone... |
15 | error_log($email, 1,'you@youremail.com',$headers); |
17 | // Make sure that you decide how to respond to errors (on the user's side) |
18 | // Either echo an error message, or kill the entire project. Up to you... |
19 | // The code below ensures that we only "die" if the error was more than |
21 | if( ($number !== E_NOTICE) && ($number < 2048) ) { |
22 | die("There was an error. Please try again later."); |
26 | // We should use our custom function to handle errors. |
27 | set_error_handler('nettuts_error_handler'); |
29 | // Trigger an error... (var doesn't exist) |
30 | echo $somevarthatdoesnotexist; |
原文地址: |
|