最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

聚合数据短信API服务接口PHP请求示例(附源码)

来源:动视网 责编:小采 时间:2020-11-27 14:20:05
文档

聚合数据短信API服务接口PHP请求示例(附源码)

聚合数据短信API服务接口PHP请求示例(附源码):使用前你需要:①:通过http://www.juhe.cn/docs/api/id/54 申请短信API服务②:在短信模板中心,添加一个模板,并通过审核一、聚合数据(www.juhe.cn)短信API服务接口PHP请求示例源码<php header('content-type:tex
推荐度:
导读聚合数据短信API服务接口PHP请求示例(附源码):使用前你需要:①:通过http://www.juhe.cn/docs/api/id/54 申请短信API服务②:在短信模板中心,添加一个模板,并通过审核一、聚合数据(www.juhe.cn)短信API服务接口PHP请求示例源码<php header('content-type:tex
 使用前你需要:

①:通过http://www.juhe.cn/docs/api/id/54 申请短信API服务

②:在短信模板中心,添加一个模板,并通过审核

一、聚合数据(www.juhe.cn)短信API服务接口PHP请求示例源码

<?php
 
header('content-type:text/html;charset=utf-8');
class SendCode
{
 private $key;
 private $tpl_id;
 public function __construct()
 {
 $this->key = 'AppKey'; // 聚合数据后台的AppKey
 $this->tpl_id = 'tpl_id'; // 申请的短信模板ID,根据实际情况修改短信模板
 }
 public function send($mobile){
 
 if (empty($mobile)) {
 $this->show_json(-4,'手机号不能为空');
 }
 
 $code = mt_rand(100000,999999);
 $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
 $smsConf = array(
 'key' => $this->key, //您申请的APPKEY
 'mobile' => $mobile, //接受短信的用户手机号码
 'tpl_id' => $this->tpl_id, //您申请的短信模板ID,根据实际情况修改
 'tpl_value' =>'#code#='.$code.'&#company#=聚合数据' //您设置的模板变量,根据实际情况修改
 );
 
 $content = $this->juhecurl($sendUrl,$smsConf, 1); //请求发送短信
 if($content){
 $result = json_decode($content,true);
 $error_code = $result['error_code'];
 if($error_code == 0){
 //状态为0,说明短信发送成功
 $data['code'] = $code;
 $this->show_json(1, $data);
 }else{
 //状态非0,说明失败
 $msg = $result['reason'];
 $this->show_json(-3, "短信发送失败(".$error_code."):".$msg);
 }
 }else{
 //返回内容异常,以下可根据业务逻辑自行修改
 $this->show_json(-3, '请求发送短信失败');
 }
 
 }
 
 /**
 * 请求接口返回内容
 * @param string $url [请求的URL地址]
 * @param string $params [请求的参数]
 * @param int $ipost [是否采用POST形式]
 * @return string
 */
 public function juhecurl($url,$params=false,$ispost=0){
 
 $httpInfo = array();
 $ch = curl_init();
 curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
 curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.13.172 Safari/537.22' );
 curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
 curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
 curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
 if( $ispost )
 {
 curl_setopt( $ch , CURLOPT_POST , true );
 curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
 curl_setopt( $ch , CURLOPT_URL , $url );
 }
 else
 {
 if($params){
 curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
 }else{
 curl_setopt( $ch , CURLOPT_URL , $url);
 }
 }
 $response = curl_exec( $ch );
 if ($response === FALSE) {
 return false;
 }
 $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
 $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
 curl_close( $ch );
 return $response;
 
 }
 
 
 public function show_json($status = 1, $return = NULL) {
 
 $ret = array('status' => $status);
 
 if (!is_array($return)) {
 if ($return) {
 $ret['result']['message'] = $return;
 }
 
 exit(json_encode($ret));
 } else {
 $ret['result'] = $return;
 }
 
 exit(json_encode($ret));
 }
 
 
 
}

二、调用示例

<?php
$send = new SendCode();
$send->send(15113993183);

三、成功时返回status为1

更多PHP相关知识,请访问PHP中文网!

文档

聚合数据短信API服务接口PHP请求示例(附源码)

聚合数据短信API服务接口PHP请求示例(附源码):使用前你需要:①:通过http://www.juhe.cn/docs/api/id/54 申请短信API服务②:在短信模板中心,添加一个模板,并通过审核一、聚合数据(www.juhe.cn)短信API服务接口PHP请求示例源码<php header('content-type:tex
推荐度:
标签: 短信 php 接口
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top