聚合短信接口-- php
1 $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL 2 $smsConf = array( 3 'key' => $juhekey, //您申请的APPKEY 4 'mobile' => $tel, //接受短信的用户手机号码 5 'tpl_id' => $juheid, //您申请的短信模板ID,根据实际情况修改 6 'tpl_value' => '#code#=' . $smscode, //您设置的模板变量随机数,根据实际情况修改 7 ); 8 9 $content = juhecurl($sendUrl, $smsConf, 1); //请求发送短信 10 if ($content) { 11 $result = json_decode($content, true); 12 $error_code = $result['error_code']; 13 if ($error_code == 0) { 14 $return = array('status' => 1, 'msg' => '短信发送成功'); 15 } else { 16 //状态非0,说明失败 17 $return['msg'] = $result['reason']; 18 } 19 } else { 20 //返回内容异常,以下可根据业务逻辑自行修改 21 $return['msg'] = "请求发送短信失败"; 22 } 23 26 //函数相当于file_get_contents 28 function juhecurl($url, $params = false, $ispost = 0){ 29 $httpInfo = array(); 30 $ch = curl_init(); 31 curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 32 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'); 33 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); 34 curl_setopt($ch, CURLOPT_TIMEOUT, 30); 35 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 36 if ($ispost) { 37 curl_setopt($ch, CURLOPT_POST, true); 38 curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 39 curl_setopt($ch, CURLOPT_URL, $url); 40 } else { 41 if ($params) { 42 curl_setopt($ch, CURLOPT_URL, $url . '?' . $params); 43 } else { 44 curl_setopt($ch, CURLOPT_URL, $url); 45 } 46 } 47 $response = curl_exec($ch); 48 if ($response === FALSE) { 49 //echo "cURL Error: " . curl_error($ch); 50 return false; 51 } 52 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 53 $httpInfo = array_merge($httpInfo, curl_getinfo($ch)); 54 curl_close($ch); 55 return $response; 56 }
具体接口 get/post参数获取返回结果还可以参考:https://www.cnblogs.com/CHEUNGKAMING/p/5717429.html
posted on 2017-02-15 18:09 longlongcheng 阅读(2625) 评论(1) 编辑 收藏 举报