Php WeChat browser sharing settings and callback details _ php instances

Source: Internet
Author: User
This article mainly introduces the php browser sharing settings and callback. if you are interested, you can refer to the sharing with friends/sharing with friends in the center. this function should be quite common, take the example of sharing to a circle of friends. the shared content is displayed in a circle of friends in the form of a small image + a simple introduction. The details are displayed after you click it, this small image and this short introduction directly become the top priority of the CTR of the shared content. By default, this image will load the first large image in the topic of the content, and the introduction will only load one URL. This display method is still quite unsatisfactory. let's take a look at the content in the following format:

First, we need a public account and obtain the appid and appsecret.

Then, through appid and appsecret, we can exchange access_token with the platform.

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 file content or get the content of the network request $ result = json_decode ($ res, true ); // accept a JSON string and convert it to the PHP variable $ access_token = $ result ['Access _ token'];

With access_token, we can obtain a jsapi_ticket from the 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 file content or get the content of the network request $ result = json_decode ($ res, true ); // accept a JSON string and convert it to the PHP variable $ ticket = $ result ['ticket '];

Now, we are ready for the setup.

The sharing settings are implemented through wx. config.

Script wx. config ({debug: false, // enable the debugging mode. The Returned values of all called APIs are displayed in the client alert. to view the input parameters, you can open them on the pc, the parameter information is output through log and printed only on the pc end. AppId: '<? Php echo APPID;?> ', // Required. the only public ID timestamp: <? Php echo $ timestamp;?>, // Required. the signature generation timestamp nonceStr: '<? Php echo $ noncestr;?> ', // Required. the random signature string signature is generated:' <? Php echo $ signature;?> ', // Required, signature jsApiList: ['onmenusharetimeline', 'onmenushareappmessage'] // required, List of JS interfaces to be used}); script

In the middle, appid is the appid of our public account, timestamp is the current timestamp, noncestr is a random string used to generate a signature, and signature is the generated signature, jsapilist is the interface we need to use. here we can use the interfaces to share with friends and to share with friends.

Briefly list timestamp, noncestr, and the generation process of signature:

// Generate a signature // Generate a random string class RandChar {function getRandChar ($ length) {$ str = null; $ strPol = "regular"; $ max = strlen ($ strPol) -1; for ($ I = 0; $ I <$ length; $ I ++) {$ str. = $ strPol [rand (0, $ max)]; // rand ($ min, $ max) generate a random integer between min and max} return $ str ;}$ randCharObj = new RandChar (); $ noncestr = $ randCharObj-> getRandChar (16 ); $ timestamp = time (); if ($ _ SERVER ['Qu ERY_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 );

So far, we have completed a configuration of wx. config, and now we can freely set the small image and introduction content we just mentioned:

Wx. ready (function () {// share it with your friends to set wx. onMenuShareTimeline ({title: 'Test title', // share the title link: 'http: // www.baidu.com ', // share the link imgUrl: 'http: // response ', // share icon success: function () {alert ("shared successfully") ;}, cancel: function () {alert ("shared failed ");}}); // share it with your friend wx. onMenuShareAppMessage ({title: 'Test title', // share title desc: 'Test sharing description', // share description link: 'http: // www.baidu.com ', // share link imgUrl: 'http: // sharing, // share icon type: '', // share type, music, video, or link. the default value is link dataUrl: '', // if the type is music or video, a data link is provided. the default value is success: function () {alert (" shared successfully ") ;}, cancel: function () {alert ("sharing failed ");}});})

The success and cancel values are also quite common, indicating the js callback after successful sharing and the callback after canceling sharing, respectively, it is useful to make small functions such as displaying answers to psychological tests after a user shares a circle of friends.

The above is all the content of this article. I hope it will be helpful to everyone's learning, and I hope you can support your own home.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.