前天弄了个短信验证的东西,有免费的短信验证真的很不错,对于做开发的人来说是个好消息,感谢电信能力开放平台的贡献。
以下是我自己调用电信短信验证码接口做的一个短信验证类,拿出来跟大家分享一下,不好勿喷。get_access_token();
$token=$this->gettoken();
$timestamp = date('y-m-d h:i:s');
$url=http://api.189.cn/v2/dm/randcode/sendsms;
$param['app_id']= app_id=.$app_id;
$param['access_token'] = access_token=.$access_token;
$param['timestamp'] = timestamp=.$timestamp;
$param['phone']=phone=.$mobile;
$param['token']=token=.$token;
$param['randcode']=randcode=.$message;
ksort($param);
$plaintext = implode(&,$param);
$param['sign'] = sign=.rawurlencode(base64_encode(hash_hmac(sha1, $plaintext, $app_secret, $raw_output=true)));
ksort($param);
$result=$this->curl_post($url,implode(&,$param));
$resultarr=json_decode($result);
}
//获取信任码
public function gettoken(){
$app_id=c('appid');
//dump($app_id);
$app_secret=c('appsecret');
$access_token=$this->get_access_token();
$timestamp = date('y-m-d h:i:s');
$url = http://api.189.cn/v2/dm/randcode/token?;
$param['app_id']= app_id=.$app_id;
$param['access_token'] = access_token=.$access_token;
$param['timestamp'] = timestamp=.$timestamp;
ksort($param);
$plaintext = implode(&,$param);
$param['sign'] = sign=.rawurlencode(base64_encode(hash_hmac(sha1, $plaintext, $app_secret, $raw_output=true)));
ksort($param);
$url .= implode(&,$param);
//$result = curl_get($url);
$r=$this->curl_get($url);
$result=json_decode($r,true);
return $result['token'];
}
//获取访问令牌
public function get_access_token(){
$app_id=c('appid');
$app_secret=c('appsecret');
$grant_type='client_credentials';
$send = 'app_id='.$app_id.'&app_secret='.$app_secret.'&grant_type='.$grant_type;
$access_token = $this->curl_post(https://oauth.api.189.cn/emp/oauth2/v3/access_token, $send);
$access_token = json_decode($access_token, true);
return $access_token['access_token'];
}
function curl_post($url,$data){ // 模拟提交数据函数
$curl = curl_init(); // 启动一个curl会话
curl_setopt($curl, curlopt_url, $url); // 要访问的地址
curl_setopt($curl, curlopt_ssl_verifypeer, 0); // 对认证证书来源的检查
curl_setopt($curl, curlopt_ssl_verifyhost, 1); // 从证书中检查ssl加密算法是否存在
curl_setopt($curl, curlopt_useragent, $_server['http_user_agent']); // 模拟用户使用的浏览器
curl_setopt($curl, curlopt_followlocation, 1); // 使用自动跳转
curl_setopt($curl, curlopt_autoreferer, 1); // 自动设置referer
curl_setopt($curl, curlopt_post, 1); // 发送一个常规的post请求
curl_setopt($curl, curlopt_postfields, $data); // post提交的数据包
curl_setopt($curl, curlopt_cookiefile, $globals['cookie_file']); // 读取上面所储存的cookie信息
curl_setopt($curl, curlopt_timeout, 30); // 设置超时限制防止死循环
curl_setopt($curl, curlopt_header, 0); // 显示返回的header区域内容
curl_setopt($curl, curlopt_returntransfer, 1); // 获取的信息以文件流的形式返回
$tmpinfo = curl_exec($curl); // 执行操作
if (curl_errno($curl)) {
echo 'errno'.curl_error($curl);
}
curl_close($curl); // 关键curl会话
return $tmpinfo; // 返回数据
}
/**
* 模拟提交参数,支持https提交 可用于各类api请求
* @param string $url : 提交的地址
* @param array $data :post数组
* @param string $method : post/get,默认get方式
* @return mixed
*/
function curl_get($url, $data='', $method='get'){
$curl = curl_init(); // 启动一个curl会话
curl_setopt($curl, curlopt_url, $url); // 要访问的地址
curl_setopt($curl, curlopt_ssl_verifypeer, false); // 对认证证书来源的检查
curl_setopt($curl, curlopt_ssl_verifyhost, false); // 从证书中检查ssl加密算法是否存在
curl_setopt($curl, curlopt_useragent, $_server['http_user_agent']); // 模拟用户使用的浏览器
curl_setopt($curl, curlopt_followlocation, 1); // 使用自动跳转
curl_setopt($curl, curlopt_autoreferer, 1); // 自动设置referer
if($method=='post'){
curl_setopt($curl, curlopt_post, 1); // 发送一个常规的post请求
if ($data != ''){
curl_setopt($curl, curlopt_postfields, $data); // post提交的数据包
}
}
curl_setopt($curl, curlopt_timeout, 30); // 设置超时限制防止死循环
curl_setopt($curl, curlopt_header, 0); // 显示返回的header区域内容
curl_setopt($curl, curlopt_returntransfer, 1); // 获取的信息以文件流的形式返回
$tmpinfo = curl_exec($curl); // 执行操作
curl_close($curl); // 关闭curl会话
return $tmpinfo; // 返回数据
}
}
sendsms.zip ( 2.41 mb 下载:697 次 )
ad:真正免费,域名+虚机+企业邮箱=0元
