How to implement WeChat JS-SDK signature in PHP

WBOY
Release: 2023-05-13 12:42:01
Original
1504 people have browsed it

With the popularity and development of WeChat, WeChat public accounts have become the preferred platform for many companies and individuals to conduct marketing and publicity. The WeChat JS-SDK is an integral part of the development of WeChat public accounts. It can help us implement some more interactive and interesting functions, such as sharing to Moments, calling WeChat payment, etc. This article will introduce how to implement WeChat JS-SDK signature in PHP to facilitate everyone to use this function in development.

1. Introduction to WeChat JS-SDK

WeChat JS-SDK is a development tool provided by WeChat official account, which allows us to use various functions of WeChat in the official account. Through WeChat JS-SDK, we can call WeChat's API interface in the web page to realize WeChat's sharing, payment, scanning QR code, downloading and other functions, bringing a better user experience to users.

2. WeChat JS-SDK signature principle

Before using WeChat JS-SDK, we need to call the interface provided by WeChat to sign. The principle of WeChat JS-SDK signature is: based on the pre-obtained access_token and jsapi_ticket, the currently requested URL is encrypted and a signature is generated. After introducing the JS file on the front-end page, verify it through wx.config. The JS file will automatically request the back-end for signature verification. If the verification is successful, you can proceed to the next step.

3. WeChat JS-SDK signature steps

  1. Get access_token and jsapi_ticket

Before using WeChat JS-SDK signature, we need to obtain access_token and jsapi_ticket jsapi_ticket these two parameters. Among them, access_token is the only credential used by WeChat public platform when calling each interface, and jsapi_ticket is the temporary ticket used in js-sdk to call WeChat JS interface. It can be obtained in the following ways:

(1) 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));
$access_token = $res->access_token;

(2) jsapi_ticket

$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=".$access_token;
$res = json_decode(file_get_contents($url ));
$ticket = $res->ticket;

  1. Generate noncestr and timestamp

noncestr and timestamp are randomly generated strings and times stamp. Can be obtained using the following code:

$nonceStr = createNonceStr();
$timeStamp = time();

//Generate a random string
function createNonceStr($length = 16){

$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
    $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
Copy after login

}

  1. Sort the parameters and splice the string

Sort all parameters according to ASCII code from small to large and then splice them into a string. Pay attention to splicing according to the requirements in the signature algorithm, as shown in the following example:

string1 = "jsapi_ticket=" . $ticket . "&noncestr=" . $nonceStr . "×tamp=" . $timeStamp . "&url =" . $url;

  1. Encrypt the spliced ​​string with SHA1

Encrypt the spliced ​​string with SHA1 to generate signature. These parameters are then returned to the front end together. As shown in the following example:

$signature = sha1($string1);

$data = array(

"appId" => $appid,
"nonceStr" => $nonceStr,
"timestamp" => $timeStamp,
"url" => $url,
"signature" => $signature,
"jsApiList" => array(
    "onMenuShareTimeline",
    "onMenuShareAppMessage",
    "onMenuShareQQ",
    "onMenuShareWeibo",
    "hideMenuItems",
    "showMenuItems",
    "chooseWXPay"
)
Copy after login

);

$signPackage = json_encode ($data);

  1. Introduce the JS file in the front-end page and verify it

Introduce the JS file provided by WeChat in the front-end page and call wx.config for verification. After successful verification, you can use the functions provided by WeChat JS-SDK.

4. Conclusion

This article introduces how to implement WeChat JS-SDK signature in PHP. I believe everyone has mastered the basic method. When using WeChat JS-SDK, you also need to make relevant configurations and calls based on actual needs. I hope this article can help you better use WeChat public accounts for marketing promotions.

The above is the detailed content of How to implement WeChat JS-SDK signature in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template