Ten extremely useful PHP code snippets of isolated 1. send text messages to call TextMagicAPI. IncludetheTextMagicPHPlibrequire (textmagic-sms-api-phpTextMagicAPI.php); Settheusernameandpasswordinformation $ isolated ten extremely useful PHP code snippets
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 = '192. 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 webpage
$ 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 Faceboo ** Number of filaments
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 image.
$ I = imagecreatefromjpeg ("image.jpg ");
For ($ x = 0; $ x For ($ y = 0; $ y $ Rgb = imagecolorat ($ I, $ x, $ y );
$ R = ($ rgb> 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
Initialize: 361400 bytes
*/
// Http://www.baoluowanxiang.com/
// Let's use up some memory
For ($ I = 0; I I <100000; $ I ++ ){
$ Array [] = md5 ($ I );
}
// Let's remove half of the array
For ($ I = 0; I 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 ('/', $ 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. ",
"TV" => "whois. TV ",
"Ru" => "whois.ripn.net ",
"Org" => "whois.pir.org ",
"Aero" => "whois. information. aero ",
"Nl" => "whais. 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 to '. $ nic_server .'! ');}
Return $ output;
}
10. sending PHP errors via Email
// Our custom error handler
Function nettuts_error_handler ($ number, $ message, $ file, $ line, $ vars ){
$ Email ="
An error ($ number) occurred on line
$ LineAnd inFile: $ 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 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
// 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;