Wechat page sharing function jssdk
1, first of all, there must be a certified public number, subscription number or service number.
2. Bind JS interface security domain name
3. Introduce the js file provided by wechat
<script type="text/javascript" src="./js/jquery.min.js" ></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.4.0.js " charset="utf-8"></script>
4. In the front-end part, only js sharing code is posted here. Other codes can be used at will:
<script type="text/javascript"> //WeChat share var url = window.location.href.split('#')[0]; var API_URLS ='http://music.szplhy.com/'; $.getJSON(API_URLS+'/Wechat/sample.php?url='+encodeURIComponent(url), function(json){ console.log(json); var params = { debug: false, appId: json.appId, timestamp: json.timestamp, nonceStr: json.nonceStr, signature: json.signature, jsApiList: [ 'onMenuShareTimeline', 'onMenuShareAppMessage', ] }; wx.config(params); var title = '2018 Special topics of the second world UAV conference and the Third Shenzhen International unmanned system Expo'; var desc = '2018 June 22-24 Shenzhen Convention and Exhibition Center is waiting for you!'; var link = 'http://music.szplhy.com/'; var imgUrl = 'http://cn-shenzhen.aliyuncs.com/uploadfile/wrj.jpg'; wx.ready(function(){ //Share with friends wx.onMenuShareTimeline({ title: title, desc: desc, link: link, imgUrl: imgUrl }); //Share with friends wx.onMenuShareAppMessage({ title: title, desc: desc, link: link, imgUrl: imgUrl }); }); wx.error(function(res){ console.log(res); }); }); </script>
Note that if the link is bound to a secure domain name, otherwise the title, description and picture will not be displayed
Back end php file (php requested by js json above)
Here, you need to pass in appid and AppSecret of two parameters wechat
<?php require_once "jssdk.php"; //Check the correctness of initialization parameters header("Access-Control-Allow-Origin:*");//Allow all sources header("Access-Control-Allow-Method:*");//Access allowed $jssdk = new JSSDK("appid", "AppSecret"); $signPackage = $jssdk->GetSignPackage($_GET['url']); echo json_encode($signPackage,true);exit();
Class file jssdk.php does not use task framework
<?php class JSSDK { private $appId; private $appSecret; public function __construct($appId, $appSecret) { $this->appId = $appId; $this->appSecret = $appSecret; } public function getSignPackage($url='') { $jsapiTicket = $this->getJsApiTicket(); // Note that the URL must be obtained dynamically, not hardcode $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $url = $url; $timestamp = time(); $nonceStr = $this->createNonceStr(); // Here, the order of parameters should be in ascending order of key value ASCII code $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; $signature = sha1($string); $signPackage = array( "appId" => $this->appId, "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string ); return $signPackage; } private 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; } private function getJsApiTicket() { // Jsapi "ticket should be stored and updated globally. The following code is written to the file as an example $data = json_decode(file_get_contents("jsapi_ticket.json")); if ($data->expire_time < time()) { $accessToken = $this->getAccessToken(); // If it's enterprise number, get ticket with the following URL // $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("jsapi_ticket.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $ticket = $data->jsapi_ticket; } return $ticket; } private function getAccessToken() { // Access? Token should be stored and updated globally. The following code is written to the file for example $data = json_decode(file_get_contents("access_token.json")); if ($data->expire_time < time()) { // If it's an enterprise number, use the following URL to obtain the access'u 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("access_token.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $access_token = $data->access_token; } return $access_token; } private 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; } }