thinkphp与微信JSSDK结合分享实例
新建CommonController.class.php来获取
appId: '', // 必填,公众号的唯一标识
timestamp: , // 必填,生成签名的时间戳
nonceStr: '', // 必填,生成签名的随机串
signature: '',// 必填,签名<?php <br />
namespace Home\Controller;<br>
use Think\Controller;<br>
<br>
<br>
class CommonController extends Controller {<br>
public function _initialize()<br>
{<br>
$jssdk = new JSSDK("您的ID", "您的ACCESS");<br>
$signPackage = $jssdk->GetSignPackage();<br>
$this->assign('signPackage',$signPackage);<br>
}<br>
}<br>
<br>
//微信方法<br>
<br>
class JSSDK {<br>
private $appId;<br>
private $appSecret;<br>
public function __construct($appId, $appSecret) {<br>
$this->appId = $appId;<br>
$this->appSecret = $appSecret;<br>
}<br>
public function getSignPackage() {<br>
$jsapiTicket = $this->getJsApiTicket();<br>
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";<br>
$timestamp = time();<br>
$nonceStr = $this->createNonceStr();<br>
// 这里参数的顺序要按照 key 值 ASCII 码升序排序<br>
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";<br>
$signature = sha1($string);<br>
$signPackage = array(<br>
"appId" => $this->appId,<br>
"nonceStr" => $nonceStr,<br>
"timestamp" => $timestamp,<br>
"url" => $url,<br>
"signature" => $signature,<br>
"rawString" => $string<br>
);<br>
return $signPackage; <br>
}<br>
private function createNonceStr($length = 16) {<br>
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";<br>
$str = "";<br>
for ($i = 0; $i
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);<br>
}<br>
return $str;<br>
}<br>
private function getJsApiTicket() {<br>
// jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例<br>
$data = json_decode(file_get_contents("jsapi_ticket.json"));<br>
if ($data->expire_time
$accessToken = $this->getAccessToken();<br>
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";<br>
$res = json_decode($this->httpGet($url));<br>
$ticket = $res->ticket;<br>
if ($ticket) {<br>
$data->expire_time = time() + 7000;<br>
$data->jsapi_ticket = $ticket;<br>
$fp = fopen("jsapi_ticket.json", "w");<br>
fwrite($fp, json_encode($data));<br>
fclose($fp);<br>
}<br>
} else {<br>
$ticket = $data->jsapi_ticket;<br>
}<br>
return $ticket;<br>
}<br>
private function getAccessToken() {<br>
// access_token 应该全局存储与更新,以下代码以写入到文件中做示例<br>
$data = json_decode(file_get_contents("access_token.json"));<br>
if ($data->expire_time
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";<br>
$res = json_decode($this->httpGet($url));<br>
$access_token = $res->access_token;<br>
if ($access_token) {<br>
$data->expire_time = time() + 7000;<br>
$data->access_token = $access_token;<br>
$fp = fopen("access_token.json", "w");<br>
fwrite($fp, json_encode($data));<br>
fclose($fp);<br>
}<br>
} else {<br>
$access_token = $data->access_token;<br>
}<br>
return $access_token;<br>
}<br>
private function httpGet($url) {<br>
$curl = curl_init();<br>
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);<br>
curl_setopt($curl, CURLOPT_TIMEOUT, 500);<br>
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);<br>
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);<br>
curl_setopt($curl, CURLOPT_URL, $url);<br>
$res = curl_exec($curl);<br>
curl_close($curl);<br>
return $res;<br>
}<br>
}
下面就可以使用了
新建IndexController.class.php<?php <br />
namespace Home\Controller;<br>
use Think\Controller;<br>
<br>
class IndexController extends CommonController {<br>
public function index(){ <br>
$this->display();<br>
}<br>
<br>
}
html文件中添加<script></script><br>
<script><br />
wx.config({<br />
appId: '{$signPackage.appId}',<br />
timestamp: {$signPackage.timestamp},<br />
nonceStr: '{$signPackage.nonceStr}',<br />
signature: '{$signPackage.signature}',<br />
jsApiList: [<br />
'checkJsApi',<br />
'onMenuShareTimeline',<br />
'onMenuShareAppMessage',<br />
'onMenuShareQQ',<br />
'onMenuShareWeibo'<br />
]<br />
}); <br />
wx.ready(function () {<br />
// 1 判断当前版本是否支持指定 JS 接口,支持批量判断<br />
wx.checkJsApi({<br />
jsApiList: [<br />
'getNetworkType',<br />
'previewImage',<br />
'onMenuShareTimeline',<br />
'onMenuShareAppMessage',<br />
'onMenuShareQQ',<br />
'onMenuShareWeibo'<br />
], <br />
});<br />
<br />
var shareData = {<br />
//标题<br />
title: '标题',<br />
//摘要<br />
desc: '摘要',<br />
//链接,可以换主页<br />
link: '{$signPackage.url}',<br />
//缩略图<br />
imgUrl: '缩略图', <br />
<br />
};<br />
wx.onMenuShareAppMessage(shareData);<br />
wx.onMenuShareTimeline(shareData);<br />
wx.onMenuShareQQ(shareData);<br />
wx.onMenuShareWeibo(shareData);<br />
});<br />
</script>
之后就大功告成
AD:真正免费,域名+虚机+企业邮箱=0元