PHP code snippets (send text messages, search for IP addresses, display the source code of the web page, check whether the server uses HTTPS, display the number of Faceboo X Silk, check the main color of the image, and obtain memory usage information)

Source: Internet
Author: User
PHP code snippets (sending text messages, searching IP addresses, displaying the source code of the web page, checking whether the server uses HTTPS, displaying the number of Faceboo ** filaments, checking the main color of the image, and obtaining memory usage information)) 1. call TextMagicAPI to send text messages. // IncludetheTextM PHP code snippet (send text messages, search for addresses based on IP addresses, display the source code of the webpage, check whether the server uses HTTPS, display the number of Faceboo ** filaments, check the main color of the image, and obtain memory usage information)
1. send a text message to call TextMagic API. // Include the TextMagic PHP librequire ('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 '2017-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. find the address function detect_city ($ IP) by ip address {$ default = 'unknown '; if (! Is_string ($ ip) | strlen ($ ip) <1 | $ ip = '2017. 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, rows => 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 webpage $ Line) {// loop thru each line and prepend line numbersecho "Line #{$ Line_num}: ". Htmlspecialchars ($ line )."
    \ N ";}4. check whether the SERVER uses HTTPSif ($ _ SERVER ['https']! = "On") {echo "This is not HTTPS";} else {echo "This is HTTPS";} 5. show Faceboo ** Number of filaments function fb_fan_count ($ facebook_name) {// Example: https://graph.facebook.com/digimantra1_data = json_decode (file_get_contents ("https://graph.facebook.com /". $ facebook_name); echo $ data-> likes;} 6. main image color: $ I = imagecreatefromjpeg ("image.jpg"); for ($ x = 0; $ x > 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); 7. obtain 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 presponorci, non ultricies elit lacus quis ante. lorem ipsum dolorsit amet, consectetur adipiscing elit. aliquamprepolicullamcorper 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 perform 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 resources and lists // like these on wikipedia ://// http://de.wikipedia.org/wiki/Whois// $ Servers = 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" => "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 "," In "=>" whois. ncst. ernet. in "," ir "=>" whois. nic. ir "," mc "=>" whois.ripe.net "," to "=>" 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 ('Error: No matching nic server found! ') ;}$ Nic_server = $ servers [$ ext]; $ output = ''; // connect to whois server: if ($ conn = fsockopen ($ nic_server, 43 )) {fputs ($ conn, $ domain. "\ r \ n"); while (! Feof ($ conn) {$ output. = fgets ($ conn, 128);} fclose ($ conn);} else {die ('Error: cocould not connect '. $ nic_server. '! ');} Return $ output;} 10. PHP error sent by Email An error ($ number) occurred on line $ LineAnd in 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, 'you @ youremail.com', $ headers); // Make sure that you 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 that we only "die" if the error was more than // just a NOTICE. if ($ number ! ==E_notice) & ($ number <2048) {die ("There was an error. please try again later. ") ;}// We shoshould 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.