PHP SMS Interface Code _php tips

Source: Internet
Author: User

This example for you to share a few common PHP SMS interface code, for your reference, the specific content as follows

1. SMS Call class

<?php/** * user:administrator * DATE:2016/5/8 0008 * Time: PM 2:36/class sms{//luosimao API Key pri
 
  Vate $_api_key = ';
 
 
  Private $_last_error = Array ();
 
  Private $_use_ssl = FALSE; Private $_ssl_api_url = Array (' Send ' => ' 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 configuration parameters * Api_key API secret key, Luosimao SMS background SMS-> trigger send below can view * Use_ssl enable HTTPS address, HTTPS has a certain performance loss, optional,  /Public Function __construct ($param = Array ()) {if (!isset ($param [' Api_key ']) is not enabled by default {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, applicable to verification code, order trigger reminder class Public function Send ($mobile, $message = ') {$api _url =! $this->_use_ssl?
    $this->_api_url[' send ']: $this->_ssl_api_url[' send '];
    $param = Array (' Mobile ' => $mobile, ' message ' => $message,);
    $res = $this->http_post ($api _url, $param);
  Return @json_decode ($res, TRUE); //Bulk send, for bulk send public function send_batch ($mobile _list = Array (), $message = Array (), $time = ') {$api _ur L = $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); }
 
  //Get SMS Account balance Public Function get_deposit () {$api _url =! $this->_use_ssl? $this->_api_url[' status ': $this->_s
    sl_api_url[' status '];
    $res = $this->http_get ($api _url);
  Return @json_decode ($res, TRUE); /** * @param string $type receive type, used to receive uplink and send status on the server side, the receiving address needs to be set in Luosimao background * @param array $param incoming parameters, obtained from the push URL, official FILE: https://luosimao.com/docs/api/*/Public Function recv ($type = ' status ', $param = Array ()) {if ($type = = ' St 
      ATUs ') {if ($param [' batch_id '] && $param [' Mobile '] && $param [' status ']] {//status/do record }}elseif ($type = = ' incoming ') {//Uplink reply if ($param [' Mobile '] && $param [' message ']) {/ Do record}}/** * @param string $api _url interface address * @param array $param post parameter * @param int $time Out timeout * @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, Curlopt_returntransfer, TRUE);
 
    curl_setopt ($ch, Curlopt_header, FALSE);
      if (Parse_url ($api _url) [' scheme '] = = ' https ') {curl_setopt ($ch, Curlopt_ssl_verifyhost, 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);
    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 timeout * @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, Curlopt_returntransfer, TRUE);
 
    curl_setopt ($ch, Curlopt_header, FALSE);
      if (Parse_url ($api _url) [' scheme '] = = ' https ') {curl_setopt ($ch, Curlopt_ssl_verifyhost, 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;
The Public Function Last_error () {return $this->_last_error;  }
}
 

2. SMS Send sample

Send single interface
 
require ' sms.php ';
$sms = new SMS (Array (' Api_key ' => ' 86f52f3ce0647dc24da53eafe29fadd4 ', ' Use_ssl ' => FALSE));
$res = $sms->send_batch (Array (' 13761428268 '), ' Captcha: 19272 ' cloud-dwelling community ');
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. Bulk Send Sample

Require ' sms.php ';
$sms = new SMS (Array (' Api_key ' => ' 86f52f3ce0647dc24da53eafe29fadd4 ', ' Use_ssl ' => FALSE));
 
 
Send Single interface
$res = $sms->send_batch (Array (' 13761428268 '), ' Captcha: 19272 ' cloud-dwelling community ');
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. Get Balance Sample

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 ' desposit: '. $res [' Deposit '];
  } else{
    Echo ' Failed,code: '. $res [' Error ']. ', msg: '. $res [' msg '];
  }
else{
  var_dump ($sms->last_error ());
}
Exit

The above is the entire content of this article, I hope 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.