This article introduces ten extremely useful PHP code snippets, each of which is wonderful and worthy of favorites. If you are interested, please refer to them.
This article introduces ten extremely useful PHP code snippets, each of which is wonderful and worthy of favorites. If you are interested, please refer to them.
We want to share some good things with you. We have shared 10 things last time, and 10 more useful PHP code snippets this time.
1. send SMS
Call 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 '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. Search for an IP address
Function detect_city ($ ip) {$ 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
<? Php // display source code
$ Lines = file ('HTTP: // google.com /');
Foreach ($ lines as $ line_num => $ line ){
// Loop thru each line and prepend line numbers
Echo "Line #{$ Line_num}: ". Htmlspecialchars ($ line )."
\ N ";
}
4. Check whether the server uses HTTPS
If ($ _ SERVER ['https']! = "On") {echo "This is not HTTPS";} else {echo "This is HTTPS ";}
5. display the number of Facebook fans
Function fb_fan_count ($ facebook_name) {// Example: Response = json_decode (file_get_contents ("https://graph.facebook.com/". $ facebook_name); echo $ data-> likes ;}
6. Check the main color of the image.
$ 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. Get memory usage information
Echo "Initial :". memory_get_usage (). "bytes \ n";/* prints Initial: 361400 bytes * // let's use up some memory for ($ I = 0; $ I <100000; $ I ++) {$ array [] = md5 ($ I);} // let'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 */
8. Use gzcompress () to compress data
$ String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. nunc ut elit id mi ultricies adipiscing. nulla facilisi. praesent pulvinar, sapien vel feugiat vestibulum, nulla dui presponorci, non ultricies elit lacus quis ante. lorem ipsum dolor sit amet, consectetur adipiscing elit. aliquam prepolicullamcorper urna quis iaculis. etiam ac massa sed turpis tempor luctus. curabitur sed nibh eu elit mollis congue. praesent ipsum diam, consectetur vitae ornare a, aliquam a nunc. in id magna pellentesque tellus posuere adipiscing. sed non mi metus, at lacinia augue. sed magna nisi, ornare in mollis in, mollis sed nunc. etiam at justo in leo congue mollis. nullam in neque eget metus hendrerit scelerisque eu non enim. ut malesuada lacus eu nulla bibendum id euismod urna sodales. "; $ compressed = gzcompress ($ string); echo" Original size :". strlen ($ string ). "\ n";/* prints Original size: 800 */echo "Compressed size :". strlen ($ compressed ). "\ n";/* prints Compressed size: 418 * // getting it back $ original = gzuncompress ($ compressed );
9. Use PHP for 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 (' http://www.jb51.net/ ', $ 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: // $ 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 "," ne T "=>" 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 "=>" whoi S. 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 "=>" w Hois. 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. Sending PHP errors via Email