Share text message code sent by PHP

Source: Internet
Author: User
Recently, php has been used to send and receive text messages, and text message notifications have been used for user orders. many text message platforms on the internet feel unreliable. I also tested a lot of code. below I will share some useful ones with you in method 1 (better and recommended)

// PHP sends SMS Monxin (PHP code function) // this code runs based on Monxin // code Source: Monxin. /config/functions. php function sms ($ config, $ language, $ pdo, $ sender, $ phone_number, $ content) {// demo var_dump (sms (self ::$ config, self :: $ language, $ pdo, "system", "18074507509,15507455992", "test content, time ". date ("H: I: s", time (); $ sender = safe_str ($ sender); $ content = safe_str ($ content ); $ arr = explode (',', $ config ['Ms'] ['Disable _ phrase ']); $ disable = false; fore Ach ($ arr as $ v) {if (strpos ($ content, $ v )! = False) {$ phrase = $ v; $ disable = true; continue ;}}if ($ disable) {return $ language ['exist _ disable_phrase ']. "". $ phrase;} $ phone_number = explode (',', $ phone_number); $ phone_number = array_unique ($ phone_number); $ addressee = ''; $ count = 0; foreach ($ phone_number as $ v) {if (preg_match ($ config ['Other'] ['Reg _ phone'], $ v) {$ addressee. = $ v. ',' ;}$ addressee = trim ($ addressee, ','); $ addressee = explode (",", $ addressee); // var _ Dump ($ addressee); $ section = ceil (count ($ addressee)/$ config ['Ms'] ['Max ']); for ($ I = 0; $ I <$ section; $ I ++) {$ phone [$ I] = ''; for ($ j = $ I * $ config ['Ms'] ['Max ']; $ j <($ I + 1) * $ config ['Ms'] ['Max ']; $ j ++) {// echo $ j. ','; if (isset ($ addressee [$ j]) {$ phone [$ I]. = $ addressee [$ j]. $ config ['SMS '] ['delimiter']; }}$ phone [$ I] = trim ($ phone [$ I], $ config ['SMS '] ['delimiter']); $ temp = explode ($ config ['SMS '] ['delimiter'], $ phone [$ I]); $ Count = count ($ temp); $ length = ceil (strlen (preg_replace ('/[\ x80-\ xff] {3}/', 'X ', $ content)/($ config ['Ms'] ['length']/2); $ count = $ length * $ count; if (! Isset ($ timing) {$ timing = 0;} if ($ phone [$ I]! = '') {$ Time = time (); $ SQL =" insert ". $ pdo-> index_pre. "phone_msg ('sender', 'address', 'content', 'State', 'time', 'count', 'timing') values ('$ sender ', '". $ phone [$ I]. "','". $ content. "', '1',' $ time', '$ count', '0')"; if ($ pdo-> exec ($ SQL )) {return send_sms ($ config, $ pdo, $ pdo-> lastInsertId ();} else {return false ;}}}}

Example 2: Use the file_get_contents function in PHP5 to send text messages (http get)

PHP code

<? Php $ url = "http://sms.api.bz/fetion.php? Username = 13812345678 & password = 123456 & sendto = 13512345678 & message = SMS content "; $ result = file_get_contents ($ url); echo $ result; // The Returned information is a Chinese character encoded by the UTF-8 by default. If your page is encoded as gb2312, use the downstream statement to output the returned information. // Echo iconv ("UTF-8", "GBK", $ result);?>

Example 3: sending text messages through curl in PHP (http post mode)

PHP code

<? Php $ data ["username"] = 13812345678; $ data ["password"] = "password123"; $ data ["sendto"] = 13512345678; $ data ["message"] = "this is a test message! "; $ Curl = new Curl_Class (); $ result = @ $ curl-> post (" http://sms.api.bz/fetion.php ", $ Data); echo $ result; // The Returned information is a Chinese character encoded by the UTF-8 by default. If your page is encoded as gb2312, use a downstream statement to output the returned information. // Echo iconv ("UTF-8", "GBK", $ result); // curl class Curl_Class {function Curl_Class () {return true;} function execute ($ method, $ url, $ fields = '', $ userAgent ='', $ httpHeaders = '', $ username ='', $ password = '') {$ ch = Curl_Class :: create (); if (false ===$ ch) {return false;} if (is_string ($ url) & strlen ($ url )) {$ ret = curl_setopt ($ ch, CURLOPT_URL, $ url);} else {return false;} // whether to display Header information curl_setopt ($ ch, CURLOPT_HEADER, false); // curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); if ($ username! = '') {Curl_setopt ($ ch, CURLOPT_USERPWD, $ username. ':'. $ password) ;}$ method = strtolower ($ method); if ('post' ==$ method) {curl_setopt ($ ch, CURLOPT_POST, true ); if (is_array ($ fields) {$ sets = array (); foreach ($ fields AS $ key => $ val) {$ sets [] = $ key. '= '. urlencode ($ val) ;}$ fields = implode ('&', $ sets);} curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ fields );} else if ('put' = $ method) {cur Rochelle setopt ($ ch, CURLOPT_PUT, true);} // curl_setopt ($ ch, CURLOPT_PROGRESS, true); // curl_setopt ($ ch, CURLOPT_VERBOSE, true ); // curl_setopt ($ ch, CURLOPT_MUTE, false); curl_setopt ($ ch, CURLOPT_TIMEOUT, 10); // Set curl timeout seconds if (strlen ($ userAgent )) {curl_setopt ($ ch, CURLOPT_USERAGENT, $ userAgent);} if (is_array ($ httpHeaders) {curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ httpHeaders );} $ ret = curl_exec ($ ch); I F (curl_errno ($ ch) {curl_close ($ ch); return array (curl_error ($ ch), curl_errno ($ ch);} else {curl_close ($ ch ); if (! Is_string ($ ret) |! Strlen ($ ret) {return false;} return $ ret;} function post ($ url, $ fields, $ userAgent = '', $ httpHeaders = '', $ username = '', $ password ='') {$ ret = Curl_Class: execute ('post', $ url, $ fields, $ userAgent, $ httpHeaders, $ username, $ password); if (false ===$ ret) {return false;} if (is_array ($ ret) {return false;} return $ ret ;} function get ($ url, $ userAgent = '', $ httpHeaders ='', $ usernam E = '', $ password ='') {$ ret = Curl_Class: execute ('GET', $ url, '', $ userAgent, $ httpHeaders, $ username, $ password); if (false ===$ ret) {return false;} if (is_array ($ ret) {return false;} return $ ret;} function create () {$ ch = null; if (! Function_exists ('curl _ init ') {return false;} $ ch = curl_init (); if (! Is_resource ($ ch) {return false;} return $ ch ;}}

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.