PHP implementation to send SMS function code

Source: Internet
Author: User
Tags urlencode ssl certificate
When developing a Web or mobile app, you'll often encounter the need to send SMS to the user, either because of login reasons, or to send a message. The following PHP code implements the ability to send SMS.
In order to send SMS in any language, an SMS gateway is required. Most SMS will provide an API, here is the use of MSG91 as SMS gateway.

function Send_sms ($mobile, $msg) {$authKey = "xxxxxxxxxxx";d ate_default_timezone_set ("Asia/kolkata"); $date = Strftime ("%y-%m-%d%h:%m:%s");//multiple mobiles numbers separated by comma$mobilenumber = $mobile; Sender Id,while using route4 sender ID should be 6 characters long $senderId = "Ikoonk"; Your message to send, the ADD URL encoding here. $message = UrlEncode ($msg); Define route $route = "template";//prepare you post Parameters$postdata = Array (' authkey ' = = $authKey, ' Mobile s ' = = $mobileNumber, ' message ' + $message, ' sender ' and ' $senderId, ' route ' and ' $route '); API url$url= "https://control.msg91.com/sendhttp.php"; init the resource$ch = Curl_init (); Curl_setopt_array ($ch, array (Curlopt_url = = $url, Curlopt_returntransfer =  > True, Curlopt_post = true, Curlopt_postfields = $postData//,curlopt_followlocation = true)); Ignore SSL Certificate verificationcurl_setopt ($ch, curlopt_ssl_verifyhost, 0); curl_setopt ($ch, CUrlopt_ssl_verifypeer, 0); Get response$output = curl_exec ($ch);//print error if Anyif (Curl_errno ($ch)) {echo ' ERROR: '. Curl_error ($ch);} curl_ Close ($ch);}

where "$authKey =" xxxxxxxxxxx ";" Need you to enter your password, "$senderId =" Ikoonk ";" You are required to enter your SenderID. You need to specify the country code when you enter a mobile number (for example, the United States is 1, India is 91).
Grammar:

<?php$message = "Hello World"; $mobile = "918112998787"; Send_sms ($mobile, $message);? >

2. Send mail using Mandrill
The Mandrill is a powerful SMTP provider. Developers tend to use a third-party SMTP provider to get better delivery of their shipments.
In the following function, you need to put "mandrill.php" in the same folder as the PHP file so that you can use TA to send the message.

function Send_email ($to _email, $subject, $message 1) {require_once ' mandrill.php '; $apikey = ' xxxxxxxxxx ';//specify your API Key Here$mandrill = new Mandrill ($apikey); $message = new StdClass (), $message->html = $message 1; $message->text = $message 1; $message->subject = $subject; $ Message->from_email = "blog@koonk.com";//sender email$message->from_name  = "Koonk";//sender name$message- >to = Array ("e-mail" = $to _email)); $message->track_opens = true; $response = $mandrill->messages->send ($message);} " $apikey = ' xxxxxxxxxx '; Specify your API key here "requires you to specify your API keys (obtained from the Mandrill account).

Grammar:

<?php$to = "abc@example.com"; $subject = "This is a test email"; $message = "Hello world!"; Send_email ($to, $subject, $message);? >

In order to achieve the best results, it is best to follow the Mandrill tutorial to configure DNS.
3. PHP function: Block SQL injection
SQL injection or SQLi common attack site means, use the code below to help you prevent these injections.

function Clean ($input) {    if (Is_array ($input))    {        foreach ($input as $key = = $val)         {            $output [$ Key] = clean ($val);            $output [$key] = $this->clean ($val);        }    }    else    {        $output = (string) $input;        If Magic quotes is on and use strip slashes        if (GET_MAGIC_QUOTES_GPC ())         {            $output = stripslashes ($OUTPU t);        }        $output = Strip_tags ($output);        $output = Htmlentities ($output, ent_quotes, ' UTF-8 ');    } Return the clean text    return $output;

}
Grammar:

<?php$text = "<script>alert (1) </script>"; $text = Clean ($text); Echo $text; >


4. Detecting User Locations
Use the following function to detect which city the user is accessing your site in.

function Detect_city ($ip) {$default = ' UNKNOWN '; $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 (' {<li>city: ([^<]*) </li>}i ', $contENT, $regs)) {$city = $regs [1]; } if (Preg_match (' {<li>state/province: ([^<]*) </li>}i ', $content, $regs)) {$state =        $regs [1]; if ($city! = "&& $state! =") {$location = $city. ', ' .          $state;        return $location;         }else{return $default; }            }

Grammar:

<?php$ip = $_server[' remote_addr ']; $city = detect_city ($IP); Echo $city; >


5. Get the source code for the Web page
Use the following function to get the HTML code for any Web page.

function Display_sourcecode ($url) {$lines = file ($url); $output = ""; foreach ($lines as $line _num = + $line) {     //loop Thru each line and prepend line numbers    $output. = "line #<b>{$line _num}</b>:". Htmlspecialchars ($line). "<br>\n";}} Syntax: <?php$url = "http://blog.koonk.com"; $source = Display_sourcecode ($url); Echo $source; >

6. Calculate the users who like your Facebook page

function Fb_fan_count ($facebook _name) {    $data = Json_decode (file_get_contents ("https://graph.facebook.com/"). $ Facebook_name));    $likes = $data->likes;    return $likes;}

Grammar:

<?php$page = "Koonktechnologies"; $count = Fb_fan_count ($page); Echo $count; >


7. Determine the dominant color of any image

function Dominant_color ($image) {$i = Imagecreatefromjpeg ($image); for ($x =0; $x <imagesx ($i); $x + +) {for    ($y =0;$ Y<imagesy ($i); $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);}

8. Whois query
Use the following function to get the full details of any domain name user.

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" and "=" whoi S.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" and "whois.iana.org", "ac" = "Whois.ni"C.ac "," ae "=" whois.uaenic.ae "," at "=" whois.ripe.net "," au "and" 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" and "whois.nic.fr", "Hu" and "whois.nic.hu", "ie" = "whois.domain registry.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 "=" whois.tv "," Ru "=& Gt "Whois.ripn.net", "org" = "whois.pir.org", "aero" = "Whois.information.aero", "NL" and "Who Is.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;}

Grammar:

<?php$domain = "http://www.blog.koonk.com"; $result = Whois_query ($domain);p rint_r ($result);? >


9. Verify your email address
Sometimes, when filling out a form on a website, the user may enter the wrong email address, which verifies that the email address is valid.

function Is_validemail ($email) {$check = 0;if (Filter_var ($email, Filter_validate_email)) {$check = 1;} return $check;}

Grammar:

<?php$email = "blog@koonk.com"; $check = Is_validemail ($email); echo $check;/If the output is 1 and then e-mail is valid.? >


10. Get the user's real IP

function getrealipaddr ()  {      if (!emptyempty ($_server[' http_client_ip '))      {          $ip =$_server[' http_ Client_ip '];      }      ElseIf (!emptyempty ($_server[' http_x_forwarded_for '))      //to Check IP is pass from proxy      {          $ip =$_server[' Http_x_forwarded_for '];      }      else      {          $ip =$_server[' remote_addr ');      }      return $ip;  }

Grammar:

<?php$ip = Getrealipaddr (); Echo $ip;? >
Related Article

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.