PHP Code Snippets (send text messages, find addresses based on IP, display the source code of a webpage, check whether the server uses HTTPS, display the number of faceboo*, detect the main color of a picture, get memory usage information)

Source: Internet
Author: User
PHP snippet (send SMS, find address based on IP, display source code of Web page, check server for HTTPS, display faceboo** number, detect main color of image, get memory usage information)
1. Send SMS Call 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) [SE Nt_text] = Wake up! [Parts_count] = 1) 2.  Find address according to 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 prepend line Numbersecho ' line #{$line _num}: " . Htmlspecialchars ($line). "
    \ n ";} 4. Check if the server is using Httpsif ($_server[' https ']! = "on") {echo "This is not HTTPS";} Else{echo "This is HTTPS";} 5. Display faceboo** number of 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. Check 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*////HTTP/ www.baoluowanxiang.com///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. Use Gzcompress () to compress data $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 to do whois check 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 resource S and lists//like these on wikipedia:////http://de.wikipedia.org/wiki/Whois//$servers = Array ("Biz" = "Whois.neulev" El.biz "," "com" = "whois.internic.net", "us" = "whois.nic.us", "Coop" and "Whois.nic.coop", "info" and "=" 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" and "whois.ripe.net", "au" and "whois.aunic.net", "be" = "whois.dns.be", "bg" and "=" Whois.ripe.net "," br "=" whois.registro.br "," BZ "=" whois.belizenic.bz "," Ca "=" whois.cira.ca "," cc "and" = " whois.nic.cc "," ch "=" whois.nic.ch "," cl "=" whois.nic.cl "," cn "=" whois.cnnic.net.cn "," cz "and" = " whois.nic.cz "," de "=" whois.nic.de "," fr "=" whois.nic.fr "," Hu"= =" whois.nic.hu "," ie "=" whois.domainregistry.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 "and" = " Whois.tv "," ru "=" whois.ripn.net "," org "=" whois.pir.org "," aero "=" Whois.information.aero "," nl "and" = " 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 the file: $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, [Email protected] ', $headers),//Make sure, and decide how 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;
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.