Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The way PHP implements Wechat customization

2025-03-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

The way PHP implements Wechat customization, for this question, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

To make Wechat interface sharing function, you need to register an official account and pass authentication in the early stage. Only in this way can the interface creation be realized.

Next, I will introduce you step by step.

1. Configuration of official account development information

You need to configure the public platform configuration information before developing the sharing interface, as shown in the following figure: click "Development-basic Information" to see the official account development information. Click "Open" in the developer key to configure your own key information. After the configuration is successful, save the developer ID (AppID) and developer password (AppSecret).

2. Configure the secure domain name of JS interface

Click "Settings and Development-official account Settings", click the settings after the JS API security domain name, and add the js security domain name as required.

3. PHP sharing API class

Class wechatClass {public $AppID; public $AppSecret; public $redirect_uri; public $DbSy; public $dump_url; public $scope; public function _ construct () {parent::__construct (); $this- > AppID = "developer password (AppSecret)"; $this- > AppSecret = "developer password (AppSecret)";} public function getSignPackage () {$jsapiTicket = $this- > getJsApiTicket () / / Note that URL must be obtained dynamically, not hardcode. $protocol = (! empty ($_ SERVER ['HTTPS']) & & $_ SERVER [' HTTPS']! = = 'off' | $_ SERVER [' SERVER_PORT'] = = 443)? "https://":" http://"; $url= "{$protocol} {$_ SERVER ['HTTP_HOST']} {$_ SERVER [' REQUEST_URI']}"; $timestamp = time (); $nonceStr = $this- > createNonceStr (); / / the parameters here are sorted in ascending order by key value ASCII code $string = "jsapi_ticket= {$jsapiTicket} & noncestr= {$nonceStr} × tamp= {$timestamp} & url= {$url}" $signature = sha1 ($string); / / var_dump ($signature); die; $signPackage = array ("appId" = > $this- > AppID, "nonceStr" = > $nonceStr, "timestamp" = > $timestamp, "url" = > $url, "signature" = > $signature, "rawString" = > $string) Return $signPackage;} public 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; } public function getJsApiTicket() { // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例 //echo $_SERVER['DOCUMENT_ROOT']; $data = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT']."/static/yejuzhi/js/access_token.json")); // var_dump($data->

Expire_time); die; if ($data- > expire_time)

< time()) { $accessToken = $this->

GetAccessToken (); / / for enterprise, use the following URL to get ticket / / $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; $url =" https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token={$accessToken}"; $res = json_decode ($this- > httpGet ($url)) $ticket = $res- > ticket; if ($ticket) {$data- > expire_time = time () + 7000; $data- > jsapi_ticket = $ticket; $fp = fopen ($_ SERVER ['DOCUMENT_ROOT']. "/ static/yejuzhi/js/access_token.json", "w"); fwrite ($fp, json_encode ($data)) Fclose ($fp);}} else {$ticket = $data- > jsapi_ticket;} return $ticket } public function getAccessToken () {/ / access_token should be stored and updated globally, the following code to write to a file as an example $data = json_decode (file_get_contents ($_ SERVER ['DOCUMENT_ROOT']. "/ static/yejuzhi/js/access_token.json")); if ($data- > expire_time)

< time()) { // 如果是企业号用以下URL获取access_token // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->

AppId&corpsecret=$this- > appSecret "; $url =" https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->AppID}&secret={$this->AppSecret}"; $res = json_decode ($this- > httpGet ($url)); $access_token = $res- > access_token; if ($access_token) {$data- > expire_time = time () + 7000 $data- > access_token = $access_token; $fp = fopen ($_ SERVER ['DOCUMENT_ROOT']. "/ static/yejuzhi/js/access_token.json", "w"); fwrite ($fp, json_encode ($data)); fclose ($fp);} else {$access_token = $data- > access_token } return $access_token;} public function httpGet ($url) {$curl = curl_init (); curl_setopt ($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt ($curl, CURLOPT_TIMEOUT, 500); curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt ($curl, CURLOPT_URL, $url) $res = curl_exec ($curl); curl_close ($curl); return $res;}}

4. PHP file configuration

I wrote it here in the TP5.1 framework, and you can set it up on your own.

$wx = new WeChatClass (); $sign = $wx- > getSignPackage (); $this- > assign ('sign',$sign)

5. Front-end HTML code

For the front-end HTML code, please refer to the development document: https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#11

This is the answer to the question about the way PHP implements Wechat customization. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report