PHP realizes wechat -- sharing Friend links

Posted by bogdani on Sat, 23 Nov 2019 17:48:56 +0100

It's not some advanced knowledge, but it took a lot of time to do it for the first time. Finally, it's deducted bit by bit. It's in use now

To solve this problem, I have encountered several bugs. 1. The generated signature is wrong. I printed it and checked it on the signature algorithm page of wechat. If it is right, it still fails. That is to get the URL link dynamically. In the end, I need to write the function in the ready function of wechat. But the document says that the function that needs to be triggered by the user can not be written in. Therefore, when all the functions are generated, I need to print them After the problem is solved, the link sent to a friend is always original. The background tries to write the function to ready to solve the problem

 

PHP
 

$this->time1 = time(); 
    $this->appId = 'xxxxxxx'; //appid of own platform
    $this->nonceStr = 'xxxxxxxx';// Secret key
    $res = file_get_contents("https://API. Weixin. QQ. COM / CGI bin / token? Grant ﹐ type = client ﹐ credential & appid = XXXX & Secret = xxxxx "); / / get token
    $ress = json_decode($res,True);
    $token = $ress['access_token'];// Take out the storage code and don't list it 
    $js = file_get_contents("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$token."&type=jsapi");
    $jss = json_decode($js,True);
    $jsapi_ticket = $jss['ticket'];//   Take out JS certificate, and do not list the storage code

  //Start the signature algorithm 
    $dataa['noncestr'] =  'sjijfdif'; //Random string will be sent to JS in a short time. It is required to be consistent 
    $dataa['jsapi_ticket'] = $jsapi_ticket;
    $dataa['timestamp'] = $this->time1;
    $this->url1 = $dataa['url'] = 'http://'. $'server ['http'host']. '$'server ['request'uri']; / / get the URL dynamically
    ksort($dataa);
    $signature = '';
    foreach($dataa as $k => $v){
     $signature .= $k.'='.$v.'&';
    }
    $signature = substr($signature, 0, strlen($signature)-1); 
    $this->signature = sha1($signature);// Required, signature, see Appendix 1

HTML

<script>
  wx.config({
   debug: false, // When debugging mode is turned on, the return values of all the APIs called will be alert ed out on the client side. To view the parameters passed in, you can open them on the pc side, and the parameter information will be printed out through the log, which will only be printed on the pc side.
   appId: "<{$appId}>", // Required, the only sign of the public number.
   timestamp:"<{$time1}>" , // Required, generate signature timestamp
   nonceStr: "<{$nonceStr}>", // Required, generate random string of signature
   signature: "<{$signature}>",// Required, signature, see Appendix 1
   jsApiList: ['onMenuShareAppMessage'] // Required, list of JS interfaces to be used, see Appendix 2 for list of all JS interfaces
  }); 
 wx.ready(function(){
  wx.onMenuShareAppMessage({
   title: 'Zhou Liang', // Share title
   desc: 'Ha ha ha ha procedural ape', // Sharing description
   link: "<{$url1}>", // Share links
   imgUrl: '/Uploads/20160921/57e24dc555372.jpg', // Share Icon
   type: 'link', // Sharing type, music, video or link, no default is link
   dataUrl: '', // If the type is music or video, you need to provide data link, which is empty by default
   success: function () { 
    alert('Share success');
   },
   cancel: function () { 
    alert('Share canceled');
   }
  });
 });
 </script>

Wechat code scanning download APP solution

 

Topics: PHP SHA1