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

Example Analysis of php Wechat browser sharing Settings and callback

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail the example analysis of php Wechat browser sharing settings and callbacks. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

The function of sharing to friends / sharing to moments in Wechat should be more commonly used. Take sharing to moments as an example. The content shared is shown to friends in the form of a small picture + a simple introduction. Click on it to show the details. This small picture and this short introduction directly become the top priority of the click-through rate of the shared content. By default, this image loads the first large image in the topic section of the content, while the profile loads only one URL. This kind of display is quite unsatisfactory, so let's take a look at the form in which this content is set. Take PHP as an example:

First of all, we need to have an official account and get appid and appsecret.

Then, through appid and appsecret, we can exchange Wechat platform for access_token.

Define ("APPID", $appid); define ("APPSECRET", $appsecret); / / get access_token$token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=". APPID. & secret=. APPSECRET;$res = file_get_contents ($token_access_url); / / get the contents of the file or network request $result = json_decode ($res, true); / / accept a string in JSON format and convert it to the PHP variable $access_token = $result ['access_token']

Through access_token, we can obtain a jsapi_ticket from Wechat platform:

/ / get jsapi_ticket$ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=TOKEN";$res = file_get_contents ($ticket_url); / / get the contents of the file or network request $result = json_decode ($res, true); / / accept a string in JSON format and convert it to the PHP variable $ticket = $result ['ticket']

All right, the work is ready, and we can start our setup.

The sharing setting of Wechat is done through wx.config.

Wx.config ({debug: false, / / enables debug mode. The returned values of all api called will be displayed in the alert on the client side. To view the parameters passed, you can open them on the PC side, and the parameter information will be typed out through log and printed only on the PC side. AppId:'', / / required, the unique ID of the official account timestamp:, / / required, the timestamp for generating the signature nonceStr:', / / required, the random string for generating the signature signature:', / / required, the signature jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage'] / / required, list of JS interfaces to be used})

The middle appid is our Wechat official account. Appid,timestamp is the current timestamp, noncestr is a random string, is used to generate signatures, signature is the generated signature, and jsapilist is the Wechat API we need to use. Here, we can use the two APIs: sharing to friends and sharing to moments.

A simple list of timestamp,noncestr and the generation process of signature:

/ / generate signature / / generate random string class RandChar {function getRandChar ($length) {$str = null; $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"; $max = strlen ($strPol)-1; for ($iS0 / strPol) (16); $timestamp = time (); if ($_ SERVER ['QUERY_STRING']) {$url =' http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']; } else {$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];}$parameters = array ("noncestr" = > $noncestr, "jsapi_ticket" = > $ticket, "timestamp" = > $timestamp, "url" = > $url); ksort ($parameters); $string1 = "; foreach ($parameters as $key = > $val) {$string1. = $key." = ". $val.";} $string1 = substr ($string1,0,-1) $signature = sha1 ($string1)

At this point, we have completed a configuration of wx.config, and then we are free to set the small images and introduction content we just mentioned:

Wx.ready (function () {/ / share to moments setting wx.onMenuShareTimeline ({title: 'test title', / / sharing title link: 'http://www.baidu.com', / / sharing link imgUrl:' https://cache.yisu.com/upload/information/20201209/266/40137.png', / / sharing icon success: function () {alert ("sharing success") }, cancel: function () {alert ("sharing failed");}}) / / share to friends wx.onMenuShareAppMessage ({title: 'test title', / / sharing title desc: 'test sharing description', / / sharing description link: 'http://www.baidu.com', / / sharing link imgUrl:' https://cache.yisu.com/upload/information/20201209/266/40137.png', / / sharing icon type:'', / / sharing type, music, video or link If left empty, defaults to link dataUrl:'', / / if type is music or video, provide a data link. Default is empty success: function () {alert ("sharing success") }, cancel: function () {alert ("sharing failure");});})

As mentioned in the middle, the values of success and cancel are also quite commonly used, indicating the js callback after successful sharing and the callback after unsharing, which are used to determine that users display psychological test answers after sharing moments.

This is the end of the sample analysis of php Wechat browser sharing settings and callbacks. I hope the above can be helpful and learn more. If you think the article is good, you can share it for more people to see.

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