Home  >  Article  >  php教程  >  免费发送短信验证码

免费发送短信验证码

WBOY
WBOYOriginal
2016-06-07 11:42:061066browse

调用短信平台,免费发送短信验证码,前提是你必须注册成为短信平台开发者。获得开发者ID.
前天弄了个短信验证的东西,有免费的短信验证真的很不错,对于做开发的人来说是个好消息,感谢电信能力开放平台的贡献。
以下是我自己调用电信短信验证码接口做的一个短信验证类,拿出来跟大家分享一下,不好勿喷。<?php <br /> <br> class PHPSMS<br> {<br> <br>  <br> <br>  <br>     public function send($mobile, $message)<br>     {<br>         $App_Id=C('AppId');<br>         $App_Secret=C('AppSecret');<br>         $access_token=$this->get_Access_token();<br>         $token=$this->getToken();<br>         $timestamp = date('Y-m-d H:i:s');<br>         $url="http://api.189.cn/v2/dm/randcode/sendSms";<br> <br>         $param['app_id']= "app_id=".$App_Id;<br>         $param['access_token'] = "access_token=".$access_token;<br>         $param['timestamp'] = "timestamp=".$timestamp;<br>         $param['phone']="phone=".$mobile;<br>         $param['token']="token=".$token;<br>         $param['randcode']="randcode=".$message;<br>         ksort($param);<br>         $plaintext = implode("&",$param);<br>         $param['sign'] = "sign=".rawurlencode(base64_encode(hash_hmac("sha1", $plaintext, $App_Secret, $raw_output=True)));<br>         ksort($param);<br> <br>         $result=$this->curl_post($url,implode("&",$param));<br>         $resultArr=json_decode($result);<br> <br>         <br> <br>     }<br>     //获取信任码<br>     public function getToken(){<br> <br>         $App_Id=C('AppId');<br>         //dump($App_Id);<br>         $App_Secret=C('AppSecret');<br>          $access_token=$this->get_Access_token();<br>          $timestamp = date('Y-m-d H:i:s');<br>         $url = "http://api.189.cn/v2/dm/randcode/token?";<br> <br>         $param['app_id']= "app_id=".$App_Id;<br>         $param['access_token'] = "access_token=".$access_token;<br>         $param['timestamp'] = "timestamp=".$timestamp;<br>         ksort($param);<br>         $plaintext = implode("&",$param);<br>         $param['sign'] = "sign=".rawurlencode(base64_encode(hash_hmac("sha1", $plaintext, $App_Secret, $raw_output=True)));<br>         ksort($param);<br>         $url .= implode("&",$param);<br>         //$result = curl_get($url);<br>        <br>         $r=$this->curl_get($url);<br>         $result=json_decode($r,true);<br>         return $result['token'];<br>         <br>     }<br>     //获取访问令牌 <br>     public function get_Access_token(){<br>         <br>         $App_Id=C('AppId');<br>         $App_Secret=C('AppSecret');<br>         $grant_type='client_credentials';<br>        <br>         $send = 'app_id='.$App_Id.'&app_secret='.$App_Secret.'&grant_type='.$grant_type;<br>    <br>         $access_token = $this->curl_post("https://oauth.api.189.cn/emp/oauth2/v3/access_token", $send);<br>         $access_token = json_decode($access_token, true);<br>        <br>         return $access_token['access_token'];<br>   <br>     }<br> <br> function curl_post($url,$data){ // 模拟提交数据函数      <br>     $curl = curl_init(); // 启动一个CURL会话      <br>     curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址                  <br>     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查      <br>     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在      <br>     curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器      <br>     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转      <br>     curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer      <br>     curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求      <br>     curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包      <br>     curl_setopt($curl, CURLOPT_COOKIEFILE, $GLOBALS['cookie_file']); // 读取上面所储存的Cookie信息      <br>     curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环      <br>     curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容      <br>     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回      <br>     $tmpInfo = curl_exec($curl); // 执行操作      <br>     if (curl_errno($curl)) {      <br>        echo 'Errno'.curl_error($curl);      <br>     }      <br>     curl_close($curl); // 关键CURL会话      <br>     return $tmpInfo; // 返回数据      <br>   }<br> <br>     /**<br>      * 模拟提交参数,支持https提交 可用于各类api请求<br>      * @param string $url : 提交的地址<br>      * @param array $data :POST数组<br>      * @param string $method : POST/GET,默认GET方式<br>      * @return mixed<br>      */<br>     function curl_get($url, $data='', $method='GET'){ <br>         $curl = curl_init(); // 启动一个CURL会话<br>         curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址<br>         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查<br>         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在<br>         curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器<br>         curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转<br>         curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer<br>         if($method=='POST'){<br>             curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求<br>             if ($data != ''){<br>                 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包<br>             }<br>         }<br>         curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环<br>         curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容<br>         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回<br>         $tmpInfo = curl_exec($curl); // 执行操作<br>         curl_close($curl); // 关闭CURL会话<br>         return $tmpInfo; // 返回数据<br>     }<br>        <br> }

附件 sendSms.zip ( 2.41 MB 下载:697 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn