应用前你需求:
①:经过http://www.juhe.cn/docs/api/id/54 请求短信API效劳
②:正在短信模板中心,增加一个模板,并经过审核
1、聚合数据(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.1364.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));
}
}2、挪用示例
<?php $send = new SendCode(); $send->send(15113993183);
3、胜利时前往status为1

更多PHP相干常识,请拜访PHP中文网!
以上就是聚合数据短信API效劳接口PHP申请示例(附源码)的具体内容,更多请存眷资源魔其它相干文章!
标签: 接口 php开发教程 php开发资料 php开发自学 api 短信
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。
抱歉,评论功能暂时关闭!