Ext: 10 Super useful snippets of PHP code
1. Send SMS
Call the Textmagic API.
Include the Textmagic PHP librequire (' textmagic-sms-api-php/textmagicapi.php ');//Set the username and password inform Ation$username = ' MyUserName '; $password = ' mypassword ';//Create A new instance of Tm$router = new Textmagicapi (Array (' Use Rname ' = $username, ' password ' = $password));//Send a text message to ' 999-123-4567 ' $result = $router->send (' Wa Ke up! ', Array (9991234567), true);//Result: result Is:array ([messages] = = Array ([19896128] = 9991234567) [Sent_text] = Wake up! [Parts_count] = 1)
? 2. Find addresses based on IP
function Detect_city ($ip) {$default = ' UNKNOWN '; if (!is_string ($IP) | | strlen ($IP) < 1 | | $ip = = ' 127.0.0.1 ' | | $ip = = ' localhost ') $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_ Returntransfer = 1, curlopt_useragent = $curlopt _useragent, Curlopt_url = $url, Curlopt_timeout = 1, curlopt_referer = ' http:/'. $_server[' Http_host '],); 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 (' {
City: ([^<]*)}i ', $content, $regs)) {$city = $regs [1]; } if (Preg_match (' {
State/province: ([^<]*)}i ', $content, $regs)) {$state = $regs [1]; if ($city! = "&& $state! =") {$location = $city. ', ' . $state; return $location; }else{return $default; } }
? 3. Display the source code of the Web page
$line) {//loop thru each line and prepend line Numbersecho ' line #{$line _num} : '. Htmlspecialchars ($line). "
\ n ";}
? 4. Check if the server is using HTTPS
if ($_server[' https ']! = "on") {echo "This is not HTTPS";} Else{echo "This is HTTPS";}
? 5. Show the number of Facebook fans
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;}
? 6. Detect the main color of the picture
$i = Imagecreatefromjpeg ("Image.jpg"); for ($x =0; $x
>) & 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);
? 7. Get Memory usage information
echo "Initial:". Memory_get_usage (). "Bytes \ n";/* printsinitial:361400 bytes*///Let's use up some memoryfor ($i = 0; $i < 100000; $i + +) {$array []= MD5 ($i);} Let's remove half of the arrayfor ($i = 0; $i < 100000; $i + +) {unset ($array [$i]);} echo "Final:". Memory_get_usage (). "Bytes \ n";/* printsfinal:885912 Bytes*/echo "Peak:". Memory_get_peak_usage (). " Bytes \ n "/* printspeak:13687072 bytes*/
? 8. Compressing data using gzcompress ()
$string = "Lorem ipsum dolor sit amet, consecteturadipiscing elit. Nunc ut elit id mi ultriciesadipiscing. Nulla Facilisi. Praesent pulvinar,sapien vel feugiat vestibulum, nulla dui pretium orci,non ultricies elit lacus quis ante. Lorem ipsum dolorsit amet, consectetur adipiscing elit. Aliquampretium ullamcorper Urna quis iaculis. Etiam ac massased turpis tempor luctus. Curabitur sed nibh eu elitmollis congue. Praesent ipsum diam, Consectetur vitaeornare A, aliquam a nunc. In ID magna pellentesquetellus posuere adipiscing. Sed non mi metus, at Laciniaaugue. Sed magna Nisi, Ornare in mollis in, mollissed nunc. Etiam at Justo in Leo Congue mollis. Nullam in Neque eget metus hendrerit scelerisqueeu non enim. Ut malesuada lacus eu nulla bibendumid euismod urna sodales. "; $compressed = Gzcompress ($string); echo" Original size: ". Strlen ($string). " \ n ";/* printsoriginal Size:800*/echo" Compressed size: ". Strlen ($compressed). " \ n ";/* printscompressed size:418*///getting it back$original = GzunCompress ($compressed);
? 9. Use PHP for whois checking
function Whois_query ($domain) {//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 $_domain = explode ('. ', $domain); $lst = count ($_domain)-1; $ext = $_domain[$lst]; You find resources and lists//like these on Wikipedia:////Http://de.wikipedia.org/wiki/Whois//$se RVers = Array ("Biz" = "whois.neulevel.biz", "com" = "whois.internic.net", "us" = "whois". Nic.us "," coop "=" Whois.nic.coop "," info "=" Whois.nic.info "," name "=" Whois.nic.name ", "NET" = "whois.internic.net", "gov" = "whois.nic.gov", "edu" = "whois.internic.net", "Mil" = "rs.internic.net", "int" and "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 "and" Whoi " S.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" and "whois.nic.hu", "ie" = "whois.domainregistr y.ie "," il "=" whois.isoc.org.il "," in "and" whois.ncst.ernet.in "," ir "=" whois.nic.ir ", "MC" = "whois.ripe.net", "to" and "whois.tonic.to", "TV" = "whois.tv", "Ru" and "WH" Ois.ripn.net "," org "=" whois.pir.org "," Aero "and" Whois.information.aero "," nl "+" whois ". domain-registry.nl " ); if (!isset ($servers [$ext])) {die (' error:no matching NIC server found! '); } $nic _server = $servers [$ext]; $output = "; Connect to whois server:if ($conn = Fsockopen ($nic _server)) {fputs ($conn, $domain. " \ r \ n "); while (!feof ($conn)) {$output. = fgets ($conn, 128); } fclose ($conn); } else {die (' error:could does connect to '. $nic _server. '!'); } return $output;}
? 10. Send php error via email
An error () occurred on line$lineAnd in thefile: $file.$message
"; $email. ="" . Print_r ($vars, 1). "
"; $headers = ' content-type:text/html; Charset=iso-8859-1 '. "\ r \ n";//Email the error to Someone...error_log ($email, 1, ' you@youremail.com ', $headers);//Make sure this decide ho W to respond to errors (on the user's side)//either echo an error message, or kill the entire project. Up to you...//the code below ensures this we only "die" if the error is more than//just a notice.if ((!== e_no TICE) && (< 2048)) {Die ("there is an error. Please try again later. ");} We should use our custom function to handle Errors.set_error_handler (' Nettuts_error_handler ');//Trigger an error ... (Var doesn ' t exist) echo $somevarthatdoesnotexist;
?
Original: http://www.oschina.net/question/28_36708