Php text message interface code and php text message interface

Source: Internet
Author: User

Php text message interface code and php text message interface

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

1. SMS class

<? Php/*** User: Administrator * Date: 2016/5/8 0008 * Time: */class Sms {// Luosimao api key private $ _ api_key = ''; private $ _ last_error = array (); private $ _ use_ssl = FALSE; private $ _ ssl_api_url = array ('send' => 'HTTP: // www.bkjia.com/v1/send.json ', 'Send _ batch' => 'HTTP: // www.bkjia.com/v1/send_batch.json', 'status' => 'HTTP: // www.bkjia.com/v1/status.json',); private $ _ api_url = array (' Send '=> 'HTTP: // www.bkjia.com/v1/send.json', 'Send _ batch' => 'HTTP: // www.bkjia.com/send_batch.json', 'status' => 'HTTP: // response ',); /*** @ param array $ param Configuration Parameter * api_key api secret key. In the luosimao text message background text message-> triggered sending, you can view * use_ssl to enable HTTPS address. HTTPS may suffer performance loss, optional./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://luosimao.com/docs/api/ */public function recv ($ type = 'status', $ param = array () {if ($ type = 'status ') {if ($ param ['batch _ id'] & $ param ['mo Bile '] & $ 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 function 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 [customer's house] '); 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 [customer's house] '); 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.