这段时间公司的项目中有用到分享自定义标题和图片的东西,从微信官方文档中认真看了一下 其实特别简单,可能对新手来讲会有点绕,这里我用PHP来讲明一下。php
我从微信官方文档中下载的sample说明,看了一下,里面的分享页是用PHP格式的,可是个人项目中是html格式的,因此我想着不改动的格式的话怎么来实现,因而从网上那种趴,结果发现答案五花八门,都是说要改为PHP的;还有就是须要后台配合的,下面给你们讲的是将php写在本地的,在界面中直调取就好;废话不说啦,直接上图html
上面是个人目录结构 wxjdk是我从sample中下载的拿的php里面的东西ajax
access_token.php 、 jsapi_ticket.php 、jssdk直接拿到过来就好json
jssdk 说一下我作修改的部分及注意点api
<?php class JSSDK { private $appId; private $appSecret; public function __construct($appId, $appSecret) { $this->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"; $this->set_log_file("[getSignPackage]".$string); $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 应该全局存储与更新,如下代码以写入到文件中作示例 $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?type=jsapi&access_token=$accessToken"; $res = json_decode($this->httpGet($url)); $this->set_log_file("[getJsApiTicket]".json_encode($res)); $ticket = $res->ticket; 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; } private 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 = json_decode($this->httpGet($url)); $this->set_log_file("[getAccessToken]".json_encode($res)); $access_token = $res->access_token; 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; } private function httpGet($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); // 为保证第三方服务器与微信服务器之间数据传输的安全性,全部微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。 // 若是在部署过程当中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。 //从官方下载的jssdk中下面这俩应该是true,我这给改为false啦,由于个人分享是http开头的,两种状况下,若是你分享中jsapi_ticket和 access_token中写不进去值,先看一下你的是分享连接是什么开头的,http的话就改为false 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; } private function get_php_file($filename) { return trim(substr(file_get_contents($filename), 15)); } private function set_php_file($filename, $content) { $fp = fopen($filename, "w"); fwrite($fp, "<?php exit();?>" . $content); fclose($fp); } //这里是给加了个日志,官网下载无,能够不加; public function set_log_file($content){ $logfile = "wx.log"; $content = date('Y-m-d H:i:s.u').' '.$content."\n"; $fp = fopen($logfile, "a"); fwrite($fp, $content); fclose($fp); } }
接下来是改写的一些东西
ajax_getconfig.php安全
<?php require_once "jssdk.php"; $jssdk = new JSSDK("你的Appid", "你的secret",$_GET['url']); $signPackage = $jssdk->GetSignPackage(); $signPackage['jsApiList'] = array('onMenuShareTimeline','onMenuShareAppMessage'); $signPackage['debug'] = false; echo json_encode($signPackage); ?>
function_share.js服务器
function Share(url,title,img,desc){ //须要说明的一点,在这里 ./ 表示的是你的根目录; $.get("./wxjdk/ajax_getconfig.php",{url:window.location.href},function(data) { // console.log(data) wx.config(data); wx.ready(function () { // 在这里调用 API wx.onMenuShareTimeline({ //例如分享到朋友圈的API title: title, // 分享标题 link: url, // 分享连接 imgUrl: img, // 分享图标 success: function () { alert('分享成功') }, cancel: function () { // 用户取消分享后执行的回调函数 } }); wx.onMenuShareAppMessage({ title: title, // 分享标题 desc: desc, // 分享描述 link: url, // 分享连接 imgUrl: img, // 分享图标 type: '', // 分享类型,music、video或link,不填默认为link dataUrl: '', // 若是type是music或video,则要提供数据连接,默认为空 success: function () { alert('分享成功') }, cancel: function () { // 用户取消分享后执行的回调函数 } }); wx.error(function (res) { alert(res.errMsg); //打印错误消息。及把 debug:false,设置为debug:ture就能够直接在网页上看到弹出的错误提示 }); }); }, "json" ); }
最后一步就是在an_share.html中引入微信的jweixin-1.2.0.js 和 function_share.js 而后调用Share函数啦!微信