首頁  >  文章  >  後端開發  >  PHP實作微信小程式簡訊驗證碼技巧

PHP實作微信小程式簡訊驗證碼技巧

WBOY
WBOY原創
2023-06-01 09:42:052575瀏覽

隨著微信小程式的普及,越來越多的開發者開始使用微信小程式作為開發平台,為使用者提供更便利的使用體驗。其中,在使用者註冊、登入等場景中,簡訊驗證碼是不可或缺的一環。本文主要介紹如何使用PHP實作微信小程式簡訊驗證碼的技巧。

一、取得微信access_token

在使用微信API之前,需要先取得access_token。 access_token是微信API的呼叫憑證,每次呼叫微信API時都需要在請求頭中攜帶access_token。具體取得方式如下:

  1. 在微信公眾平台中建立小程序,然後在小程式開發設定中查看appId和appSecret。
  2. 根據appId和appSecret發送HTTP GET請求,取得access_token。具體程式碼如下:
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
$res = json_decode(file_get_contents($url), true);
$access_token = $res['access_token'];

二、產生隨機驗證碼

產生隨機驗證碼的過程非常簡單,使用PHP內建的rand函數即可。具體程式碼如下:

$code = rand(1000, 9999);

三、呼叫簡訊API發送驗證碼

在取得access_token和產生隨機驗證碼後,就可以呼叫簡訊API發送驗證碼了。這裡以騰訊雲端簡訊為例,其他簡訊服務商類似。

  1. 在騰訊雲端簡訊控制台中建立應用,並取得應用程式id、應用程式key和簡訊範本id。
  2. 根據應用程式id和應用程式key產生簽名,並根據簡訊模板id和隨機驗證碼產生簡訊內容。
  3. 使用騰訊雲簡訊API發送簡訊。具體程式碼如下:
// 生成签名
$timestamp = time();
$sig = md5("appkey=".$appkey."×tamp=".$timestamp);

// 生成短信内容
$content = "【签名】验证码为:".$code.",有效期5分钟,请勿泄露。如非本人操作,请忽略本短信。";

// 发送短信
$url = "https://yun.tim.qq.com/v5/tlssmssvr/sendsms?sdkappid=".$sdkappid."&random=".rand();
$data = array(
    "tel" => array("nationcode" => "86", "mobile" => $mobile),
    "type" => 0,
    "msg" => $content
);
$data = json_encode($data);
$header = array(
    "Content-type: application/json",
    "Authorization: ".$sig,
    "Timestamp: ".$timestamp
);
$options = array(
    "http" => array(
        "method" => "POST",
        "header" => implode("
", $header),
        "content" => $data
    )
);
$res = json_decode(file_get_contents($url, false, stream_context_create($options)), true);

四、儲存驗證碼到Redis

為了確保驗證碼的有效期限,需要將產生的隨機驗證碼儲存到Redis中,設定一個有效期限。具體程式碼如下:

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->setex($mobile, 300, $code);

五、完整程式碼

function send_sms($mobile, $appid, $appsecret, $sdkappid, $appkey, $templateid)
{
    // 获取access_token
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
    $res = json_decode(file_get_contents($url), true);
    $access_token = $res['access_token'];

    // 生成随机验证码
    $code = rand(1000, 9999);

    // 发送短信
    $timestamp = time();
    $sig = md5("appkey=".$appkey."×tamp=".$timestamp);
    $content = "【签名】验证码为:".$code.",有效期5分钟,请勿泄露。如非本人操作,请忽略本短信。";
    $url = "https://yun.tim.qq.com/v5/tlssmssvr/sendsms?sdkappid=".$sdkappid."&random=".rand();
    $data = array(
        "tel" => array("nationcode" => "86", "mobile" => $mobile),
        "type" => 0,
        "msg" => $content
    );
    $data = json_encode($data);
    $header = array(
        "Content-type: application/json",
        "Authorization: ".$sig,
        "Timestamp: ".$timestamp
    );
    $options = array(
        "http" => array(
            "method" => "POST",
            "header" => implode("
", $header),
            "content" => $data
        )
    );
    $res = json_decode(file_get_contents($url, false, stream_context_create($options)), true);

    // 保存验证码到Redis
    $redis = new Redis();
    $redis->connect('127.0.0.1', 6379);
    $redis->setex($mobile, 300, $code);

    return array("code" => $code);
}

六、總結

本文介紹如何使用PHP實作微信小程式簡訊驗證碼的技巧,包括取得access_token 、產生隨機驗證碼、呼叫簡訊API發送驗證碼和保存驗證碼到Redis。這些技巧不僅適用於微信小程序,也可以用於其他類型的應用程式。希望本文對大家有幫助。

以上是PHP實作微信小程式簡訊驗證碼技巧的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn