Php text message interface code-PHP source code

Source: Internet
Author: User
This article describes in detail the php text message interface code, php text message sending, php batch sending, php balance acquisition, and other code, interested friends can refer to this article for details on php text message interface code, php text message sending, php batch sending, php getting balance and other code, for more information, see

This example shares several common php text message interface codes for your reference. The details are as follows:

1. SMS class

 ' http://www.jb51.net/v1/send.json ', 'Send _ batch' =>' http://www.jb51.net/v1/send_batch.json ', 'Status' =>' http://www.jb51.net/v1/status.json ',); Private $ _ api_url = array ('send' =>' http://www.jb51.net/v1/send.json ', 'Send _ batch' =>' http://www.jb51.net/send_batch.json ', 'Status' =>' http://www.jb51.net/v1/status.json ',);/*** @ Param array $ param configure the parameter * api_key api secret key. in the background text message of luosimao, click ** use_ssl to enable the HTTPS address, HTTPS has certain performance loss. optional. it is disabled by default */public function _ construct ($ param = array () {if (! Isset ($ param ['api _ key']) {die ("api key error. ");} if (isset ($ param ['api _ key']) {$ this-> _ api_key = $ param ['api _ key'];} if (isset ($ param ['use _ ssl ']) {$ this-> _ use_ssl = $ param ['use _ ssl'] ;}} // Trigger, single message, applicable to verification code, order trigger reminder class public function send ($ mobile, $ message = '') {$ api_url =! $ This-> _ use_ssl? $ This-> _ api_url ['put']: $ this-> _ ssl_api_url ['put']; $ param = array ('mobile' => $ mobile, 'message' => $ message,); $ res = $ this-> http_post ($ api_url, $ param); return @ json_decode ($ res, TRUE );} // batch send, used to send public function send_batch ($ mobile_list = array (), $ message = array (), $ time = '') {$ api_url =! $ This-> _ use_ssl? $ This-> _ api_url ['send _ batch']: $ this-> _ ssl_api_url ['send _ batch']; $ mobile_list = is_array ($ mobile_list )? Implode (',', $ mobile_list): $ mobile_list; $ param = array ('mobile _ list' => $ mobile_list, 'message' => $ message, 'Time' => $ time,); $ res = $ this-> http_post ($ api_url, $ param); return @ json_decode ($ res, TRUE );} // Obtain the SMS account balance public function get_deposit () {$ api_url =! $ This-> _ use_ssl? $ This-> _ api_url ['status']: $ this-> _ ssl_api_url ['status']; $ res = $ this-> http_get ($ api_url ); return @ json_decode ($ res, TRUE);}/*** @ param string $ type receiving type, used to receive upstream and sending statuses on the server side, the receiving address must be set in the luosimao background * @ param array $ param to get the parameters from the pushed url. official documentation: https://www.php1.cn/ */Public function recv ($ type = 'status', $ param = array () {if ($ type = 'status ') {if ($ param ['batch _ id'] & $ param ['mobile'] & $ param ['status']) {// status // do record} elseif ($ type = 'incoming ') {// upstream reply if ($ param ['mobile'] & $ param ['message']) {// do record }}/ *** @ param string $ api_url interface address * @ param array $ param post parameter * @ param int $ timeout time * @ return bool * /private functio N http_post ($ api_url = '', $ param = array (), $ timeout = 5) {if (! $ Api_url) {die ("error api_url") ;}$ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ api_url); curl_setopt ($ ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout); curl_setopt ($ ch, success, TRUE); curl_setopt ($ ch, CURLOPT_HEADER, FALSE ); if (parse_url ($ api_url) ['scheme '] = 'https') {curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt ($ ch, success, FALSE);} curl_setopt ($ ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt ($ ch, CURLOPT_USERPWD, 'api: key -'. $ this-> _ api_key); curl_setopt ($ ch, CURLOPT_POST, TRUE); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ param); $ res = curl_exec ($ ch ); $ error = curl_error ($ ch); curl_close ($ ch); if ($ error) {$ this-> _ last_error [] = $ error; return FALSE ;} Return $ res;}/*** @ param string $ api_url interface address * @ param string $ timeout time * @ return bool */private function http_get ($ api_url = '', $ timeout = '') {if (! $ Api_url) {die ("error api_url") ;}$ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ api_url); curl_setopt ($ ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout); curl_setopt ($ ch, success, TRUE); curl_setopt ($ ch, CURLOPT_HEADER, FALSE ); if (parse_url ($ api_url) ['scheme '] = 'https') {curl_setopt ($ ch, scheme, FALSE); curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE );} curl_setopt ($ ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt ($ ch, CURLOPT_USERPWD, 'api: key -'. $ this-> _ api_key); $ res = curl_exec ($ ch); $ error = curl_error ($ ch); curl_close ($ ch); if ($ error) {$ this-> _ last_error [] = curl_error ($ ch); return FALSE;} return $ res;} public function last_error () {return $ this-> _ last_error ;}}

2. text message sending example

// Send single-click interface require 'SMS. php '; $ sms = new Sms (array ('api _ key' => '86f52f3ce0647dc24da53eafe29fadd4', 'Use _ ssl '=> FALSE )); $ res = $ sms-> send_batch (array ('20140901'), 'verification code: 13761428268 [foot home] '); if ($ res) {if (isset ($ res ['error']) & $ res ['error'] = 0) {echo 'success ';} else {echo 'failed, code :'. $ res ['error']. ', msg :'. $ res ['MSG '] ;}} else {var_dump ($ sms-> last_error ();} exit;


3. batch sending example

Require 'SMS. php '; $ sms = new Sms (array ('api _ key' => '86f52f3ce0647dc24da53eafe29fadd4', 'Use _ ssl '=> FALSE )); // send single-click interface $ res = $ sms-> send_batch (array ('20140901'), 'verification code: 13761428268 [foot home] '); if ($ res) {if (isset ($ res ['error']) & $ res ['error'] = 0) {echo 'success ';} else {echo 'failed, code :'. $ res ['error']. ', msg :'. $ res ['MSG '] ;}} else {var_dump ($ sms-> last_error ();} exit;

4. example of obtaining balance

// Deposit balance query require 'SMS. php '; $ sms = new Sms (array ('api _ key' => '86f52f3ce0647dc24da53eafe29fadd4', 'Use _ ssl '=> FALSE )); $ res = $ sms-> get_deposit (); if ($ res) {if (isset ($ res ['error']) & $ res ['error'] = 0) {echo 'destsit :'. $ res ['posit'];} else {echo 'failed', code :'. $ res ['error']. ', msg :'. $ res ['MSG '] ;}} else {var_dump ($ sms-> last_error ();} exit;

The above is all the content of this article, hoping to help you learn.

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.