对接电信发短信接口
<?php
namespace Com\CTCC;
/**
* 模板短信发送类
* @author yangchaochao
*
*/
class SendTemplateMessage
{
const OAUTH_URL = 'https://oauth.api.189.cn';
const API_URL = 'http://api.189.cn';
const OAUTH_ACCESS_TOKEN = '/emp/oauth2/v3/access_token';
const SEND_TEMPLATE_MESSAGE = '/v2/emp/templateSms/sendSms';
private $app_id = '';
private $app_secret = '';
private $grant_type = '';
private $access_token = '';
public $errCode = 40001;
public $errMsg = "no access";
public function __construct($options)
{
$this->app_id = isset($options['app_id']) ? $options['app_id'] : '';
$this->app_secret = isset($options['app_secret']) ? $options['app_secret'] : '';
$this->grant_type = isset($options['grant_type']) ? $options['grant_type'] : '';
$this->access_token = isset($options['access_token']) ? $options['access_token'] : '';
}
/**
* 获取access_token
* @param string $app_id
* @param string $app_secret
* @param string $access_token
* @return Ambigous <string, unknown, mixed, object>
*/
public function getAccessToken($app_id = '', $app_secret = '', $grant_type = '', $access_token = '')
{
if (!$app_id || !$app_secret || !$grant_type){
$app_id = $this->app_id;
$app_secret = $this->app_secret;
$grant_type = $this->grant_type;
}
if ($access_token){
$this->access_token = $access_token;
return $this->access_token;
}
$cache_name = 'sendtemplatemessage_access_token' . $app_id;
if ($rs = S($cache_name)){
$this->access_token = $rs;
return $this->access_token;
}
$post_param = array(
'grant_type' => $grant_type
,'app_id' => $app_id
,'app_secret' => $app_secret
);
$access_token_url = self::OAUTH_URL . self::OAUTH_ACCESS_TOKEN;
$http_result = $this->http_post($access_token_url, $post_param);
if ($http_result){
$json = json_decode($http_result);
if (!$json || $json->res_code != 0){
$this->errCode = isset($json->res_code) ? $json->res_code : "-1";
$this->errMsg = isset($json->res_message) ? $json->res_message : 'json解析失败';
return false;
}else{
$this->access_token = $json->access_token;
$expire = $json->expires_in ? intval($json->expires_in)-100 : 3600;
S($cache_name,$this->access_token,$expire);
return $this->access_token;
}
}
return false;
}
/**
* 发送模板短信
* @param unknown $acceptor_tel
* @param unknown $template_id
* @param unknown $template_param
* @param unknown $timestamp
* @param string $app_id
* @return Ambigous <string, mixed>|mixed|boolean
*/
public function send($acceptor_tel, $template_id, $template_param, $timestamp, $app_id = '')
{
if (!$this->access_token && !$this->getAccessToken()){
$result['code'] = $this->errCode;
$result['message'] = $this->errMsg;
return $result;
}
if(!$app_id){
$app_id = $this->app_id;
}
$post_param = array(
'app_id' => $app_id
,'access_token' => $this->access_token
,'acceptor_tel' => $acceptor_tel
,'template_id' => $template_id
,'template_param' => json_encode($template_param)
,'timestamp' => $timestamp
);
$send_template_url = self::API_URL . self::SEND_TEMPLATE_MESSAGE;
$http_result = $this->http_post($send_template_url, $post_param);
if ($http_result){
$json = json_decode($http_result);
if(!$json || $json->res_code != 0){
$result['code'] = isset($json->res_code) ? $json->res_code : "-1";
$result['message'] = isset($json->res_message) ? $json->res_message : 'json解析失败';
return $result;
}else{
$result['code'] = $json->res_code;
$result['message'] = $json->res_message;
$result['idertifier'] = $json->idertifier;
return $result;
}
}
return false;
}
/**
* GET 请求
* @param string $url
*/
public function http_get($url){
$oCurl = curl_init();
if(stripos($url,"https://")!==FALSE){
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
if(intval($aStatus["http_code"])==200){
return $sContent;
}else{
return false;
}
}
/**
* POST 请求
* @param string $url
* @param array $param
* @param boolean $post_file 是否文件上传
* @return string content
*/
private function http_post($url,$param,$post_file=false){
$oCurl = curl_init();
if(stripos($url,"https://")!==FALSE){
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
if (is_string($param) || $post_file) {
$strPOST = $param;
} else {
$aPOST = array();
foreach($param as $key=>$val){
$aPOST[] = $key."=".urlencode($val);
}
$strPOST = join("&", $aPOST);
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($oCurl, CURLOPT_POST,true);
curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
curl_setopt($oCurl, CURLOPT_ENCODING, 'UTF-8');
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
if(intval($aStatus["http_code"])==200){
return $sContent;
}else{
return false;
}
}
}
用于ThinkPHP框架,其他框架修改下缓存方式也是可用的。