这篇文章主要为大家详细介绍了php短信接口代码,php短信发送、php批量发送、php获取余额等代码,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了几个常用的php短信接口代码,供大家参考,具体内容如下
1. 短信调用class
<?php /** * User: Administrator * Date: 2016/5/8 0008 * Time: 下午 2:36 */ class Sms{ //Luosimao api key private $_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 配置参数 * api_key api秘钥,在luosimao短信后台短信->触发发送下面可查看 * use_ssl 启用HTTPS地址,HTTPS有一定性能损耗,可选,默认不启用 */ 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']; } } //触发,单发,适用于验证码,订单触发提醒类 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 ); } //批量发送,用于大批量发送 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 ); } //获取短信账号余额 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 接收类型,用于在服务器端接收上行和发送状态,接收地址需要在luosimao后台设置 * @param array $param 传入的参数,从推送的url中获取,官方文档:http://www.php.cn/ */ public function recv( $type = 'status' , $param = array() ){ if( $type == 'status' ){ if( $param['batch_id'] && $param['mobile'] && $param['status'] ){ //状态 // do record } }elseif( $type == 'incoming' ){ //上行回复 if( $param['mobile'] && $param['message'] ){ // do record } } } /** * @param string $api_url 接口地址 * @param array $param post参数 * @param int $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 接口地址 * @param string $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; } public function last_error(){ return $this->_last_error; } }
2.短信发送示例
//send 单发接口 require 'sms.php'; $sms = new Sms( array('api_key' => '86f52f3ce0647dc24da53eafe29fadd4' , 'use_ssl' => FALSE ) ); $res = $sms->send_batch( array('13761428268') , '验证码:19272【脚本之家】'); 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.批量发送示例
require 'sms.php'; $sms = new Sms( array('api_key' => '86f52f3ce0647dc24da53eafe29fadd4' , 'use_ssl' => FALSE ) ); //send 单发接口 $res = $sms->send_batch( array('13761428268') , '验证码:19272【脚本之家】'); 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.获取余额示例
//deposit 余额查询 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;
以上就是本文的全部内容,希望对大家的学习有所帮助。
성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사
R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
2 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
Repo : 팀원을 부활시키는 방법
1 몇 달 전By尊渡假赌尊渡假赌尊渡假赌
헬로 키티 아일랜드 어드벤처 : 거대한 씨앗을 얻는 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
스플릿 소설을이기는 데 얼마나 걸립니까?
3 몇 주 전ByDDD
R.E.P.O. 파일 저장 위치 : 어디에 있고 그것을 보호하는 방법은 무엇입니까?
3 몇 주 전ByDDD

뜨거운 도구

안전한 시험 브라우저
안전한 시험 브라우저는 온라인 시험을 안전하게 치르기 위한 보안 브라우저 환경입니다. 이 소프트웨어는 모든 컴퓨터를 안전한 워크스테이션으로 바꿔줍니다. 이는 모든 유틸리티에 대한 액세스를 제어하고 학생들이 승인되지 않은 리소스를 사용하는 것을 방지합니다.

SublimeText3 Linux 새 버전
SublimeText3 Linux 최신 버전

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)
