Share a few very useful PHP snippet _php Tutorials

Source: Internet
Author: User
Tags php error vars
Call the textmagic++ API.
Include the textmagicphp Lib
Require (' textmagic-sms-api-php/textmagicapi.php ');
Set the username Andpassword information
$username = ' MyUserName ';
$password = ' MyPassword ';
Create a new instanceof TM
$router = new Textmagicapi (Array (
' Username ' = $username,
' Password ' = $password
));
Send a text message to ' 999-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 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
$lines = File (' http://google.com/');
foreach ($lines as$line_num = $line) {
Loop thru prepend Line numbers
echo "line # {$line _num}:". Htmlspecialchars ($line). "
\ n ";
}
4. Check if the server is using HTTPS
if ($_server[' HTTPS ']!= "on") {
echo "This was 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
for ($y =0; $y
$rgb = Imagecolorat ($i, $x, $y);
$r = ($rgb >>) & 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. Compressing data using gzcompress ()
$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 pretiumorci,
Non ultricies elit lacus quis ante. Lorem ipsum dolor
Sit amet, consectetur adipiscing elit. Aliquam
Pretium ullamcorper 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 Justoin 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 to do 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
//
$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:could does connect to '. $nic _server. '!'); }
return $output;
}
10. Send PHP Error via email
Our custom error handler
function Nettuts_error_handler ($number, $message, $file, $line, $vars) {
$email = "
An error ($number) occurred on line
$line and 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, ' you@youremail.com ', $headers);
Make sure, 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 is more than
Just a NOTICE.
if (($number!== e_notice) && ($number < 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 ... (VARDOESN ' t exist)
Echo$somevarthatdoesnotexist;

From the Battlefield diary _lamp brother Lian

http://www.bkjia.com/PHPjc/478189.html www.bkjia.com true http://www.bkjia.com/PHPjc/478189.html techarticle call the textmagic++ API. Include the textmagicphp lib require (textmagic-sms-api-php/textmagicapi.php); Set the username andpassword information $username = MyUserName; $pass ...

  • 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.