有时候当一个项目或工程需要微信端分享之后做一系列事件那么我们就需要获取到微信分享这个动作,也就是说我们已经知道了当前这个东西已经被分享了 ,那么走微信默认的分享显然是不行的我们需要自己动手来配置微信分享,当用户分享时走的是我们预定好的程序那么我们就可以轻松实现分享之后所做的事情。
在页面端主要是一个js如下:
当wx.config配置好后程序才会接着走下面的分享,此处调试可将false改成true若配置好那么正常弹出ok等信息 若未配置好 那么此处改成true也不会有任何弹出效果
wx.config需要控制器传来四个参数即appId、timestamp、nonceStr、signature;控制器代码如下:
<?php
$jssdk = new \Home\Util\JSSDK(C('APPID'), C('SECRET'));//此处C里面的东西为你所使用的公众号的appid和secret,这俩个东西可在微信公众平台获取到 不详细解释 找不到追加评论(讲解) JSSDK文件代码在下
$signPackage = $jssdk->GetSignPackage();
$this->assign('appid',$signPackage["appId"]);
$this->assign('timestamp',$signPackage["timestamp"]);
$this->assign('nonceStr',$signPackage["nonceStr"]);
$this->assign('signature',$signPackage["signature"]);
在这里就配置好了wx.config,还可以接着穿title等信息 在这里举个例子
$this->assign('title',$title);
JSSDK文件代码:
appId = $appId;
$this->appSecret = $appSecret;
}
public function getSignPackage() {
$jsapiTicket = $this->getJsApiTicket();
// 注意 URL 一定要动态获取,不能 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();
// 这里参数的顺序要按照 key 值 ASCII 码升序排序
$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;
}
public function getJsApiTicket() {
// jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例
$data = json_decode($this->get_php_file("jsapi_ticket.php"));
if ($data->expire_time < time()) {
$accessToken = $this->getAccessToken();
// 如果是企业号用以下 URL 获取 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?access_token=$accessToken&type=jsapi";
$res = json_decode($this->httpGet($url));
$ticket = $res->ticket;
// var_dump($url);
if ($ticket) {
$data->expire_time = time() + 7000;
$data->jsapi_ticket = $ticket;
$this->set_php_file("jsapi_ticket.php", json_encode($data));
}
} else {
$ticket = $data->jsapi_ticket;
}
return $ticket;
}
public function getAccessToken() {
// access_token 应该全局存储与更新,以下代码以写入到文件中做示例
$data = json_decode($this->get_php_file("access_token.php"));
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 = $this->getJson($url);
$access_token = $res['access_token'];
// var_dump($res);
if ($access_token) {
$data->expire_time = time() + 7000;
$data->access_token = $access_token;
$this->set_php_file("access_token.php", json_encode($data));
}
} else {
$access_token = $data->access_token;
}
return $access_token;
// $aa = $access_token;
// var_dump($aa);
}
//获取access_token
public function getJson($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
// var_dump(json_decode($output, true));
return json_decode($output, true);
}
//获取ticket
private function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($curl, CURLOPT_TIMEOUT, 500);
// 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
// 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
$res = curl_exec($curl);
// var_dump($res);
curl_close($curl);
return $res;
}
private function get_php_file($filename) {
return trim(substr(file_get_contents($filename), 15));
// echo trim(substr(file_get_contents($filename), 15));die;
// $aa = trim(substr(file_get_contents($filename), 15));
}
private function set_php_file($filename, $content) {
$fp = fopen($filename, "w");
fwrite($fp, "" . $content);
fclose($fp);
}
}
如果我们想动态传递参数怎么办?你可以参考这篇文章:https://www.cwhello.com/4589.html
本文来自投稿,不代表科技代码立场,如若转载,请注明出处https://www.cwhello.com/4585.html
如有侵犯您的合法权益请发邮件951076433@qq.com联系删除