signature invalid1求助 弄了好久 不知道出了什么問題 URL也是動(dòng)態(tài)獲取的
控制器
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
? ? public function index(){
//獲得參數(shù) signature nonce token timestamp echostr
? ? ? ? $nonce ? ? = $_GET['nonce'];
? ? ? ? $token ? ? = 'cdbb';
? ? ? ? $timestamp = $_GET['timestamp'];
? ? ? ? $echostr ? = $_GET['echostr'];
? ? ? ? $signature = $_GET['signature'];
? ? ? ? //形成數(shù)組,然后按字典序排序
? ? ? ? $array = array();
? ? ? ? $array = array($nonce, $timestamp, $token);
? ? ? ? sort($array);
? ? ? ? //拼接成字符串,sha1加密 ,然后與signature進(jìn)行校驗(yàn)
? ? ? ? $str = sha1( implode( $array ) );
? ? ? ? if( $str ?== $signature && $echostr ){
? ? ? ? ? ? //第一次接入weixin api接口的時(shí)候
? ? ? ? ? ? echo ?$echostr;
? ? ? ? ? ? exit;
? ? ? ? }else{
? ? ? ? }
? ? }
//獲取jsapi-ticket 全局票據(jù)
?public ? function getJsApiTicket(){
? ? ? ? //如果session中保存有效的jsapi-ticket?
? ? ? ? if($_SESSION['jsapi-ticket-expire_time']>time() && $_SESSION['jsapi-ticket']){
? ? ? ? ? ? $jsapi=$_SESSION['jsapi-ticket'];
? ? ? ? ? ? echo "aba";
? ? ? ? }else{
? ? ? ? ? ? echo "aaa";
? ? ? ? ? ? $access_token= $this->getWxAccessToken();
? ? ? ? ? ? var_dump($access_token);
? ? ? ? ? ? $url='https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$access_token.'&type=jsapi';
? ? ? ? ? ? $re=$this->http_curl($url);
? ? ? ? ? ? $jsapi=$re['ticket'];
? ? ? ? ? ? $_SESSION['jsapi-ticket']=$jsapi;
? ? ? ? ? ? $_SESSION['jsapi-ticket-expire_time']=time()+7000;
? ? ? ? }
? ? ? ? var_dump($access_token);
? ? ? ? ? ? return $jsapi;
? ? }
? /* ?public ? function a(){
? ? ? ? //如果session中保存有效的jsapi-ticket?
? ? ? ? if($_SESSION['expire_time']>time() && $_SESSION['access_token']){
? ? ? ? ? ? $access_token=$_SESSION['access_token'];
? ? ? ? }else{
? ? ? ? ? ??
? ? ? ? ? ? $appid = 'wxd1157c516b3578d8';
? ? ? ? ? ? $appsecret = ?'26afef5b19e1ebfc3382604908460aa9';
? ? ? ? ? ? $url= "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
? ? ? ? ? ? $re=$this->http_curl($url);
? ? ? ? ? ? $re = json_decode($re, true);
? ? ? ? ? ? $access_token=$re['access_token'];
? ? ? ? ? ? var_dump($access_token);
? ? ? ? ? ? $_SESSION['access_token']=$access_token;
? ? ? ? ? ? $_SESSION['expire_time']=time()+7200;
? ? ? ? }
? ? ? ?
? ? ? ??
? ? ? ? ? ? return $access_token;
? ? }*/
? ? //獲取16位隨機(jī)碼
? public ?function getRandCode(){
? ? ? ? $num=16;
? ? ? ? $array=array(
? ? ? ? ? ? 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
? ? ? ? ? ? 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
? ? ? ? ? ? '0','1','2','3','4','5','6','7','8','9'
? ? ? ? );
? ? ? ? $tmpstr='';
? ? ? ? $max=count($array);
? ? ? ? for($i=1;$i<=$num;$i++){
? ? ? ? ? ? $key = rand(0,$max-1);
? ? ? ? ? ? $tmpstr.=$array[$key];
? ? ? ? };
? ? ? ? ?
? ? ? ? ?
? ? ? ? return $tmpstr;
? ? }
? ? //js-sdk ?分享朋友圈
?public ? ?function share(){
? ? ? ? //獲取jsapi-ticket 票據(jù)
? ? ? ? $jsapi=$this->getJsApiTicket();
? ? ? ? $timestamp=time();
? ? ? ? $nonceStr=$this->getRandCode();
? ? ? ? //$url='http://114.215.134.149/dashboard/project/TP/weixin.php?a=share';
? ? ? ? $protocol = (!empty($_SERVER[HTTPS]) && $_SERVER[HTTPS] !== off || $_SERVER[SERVER_PORT] == 443) ? "https://" : "http://";
? ? ? ? ? $url = $protocol.$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
? ? ? ? ? var_dump($url);
? ? ? ? //獲取signature
? ? ? ? $signature='jsapi_ticket='.$jsapi.'&noncestr='.$nonceStr.'×tamp='.$timestamp.'&url='.$url;
? ? ? ? $signature=sha1($signature);
? ? ? ? $this->assign('name','慕課');
? ? ? ? $this->assign('timestamp',$timestamp);
? ? ? ? $this->assign('nonceStr',$nonceStr);
? ? ? ? $this->assign('signature',$signature);
? ? ? ? $this->display();
? ? }
? ?public function http_curl($url,$type='get',$res='json',$arr=''){
? ? ? ? ? ?//1.初始化curl
? ? ? ? ? ?$ch ?=curl_init();
? ? ? ? ? ?//2.設(shè)置curl的參數(shù)
? ? ? ? ? ?curl_setopt($ch,CURLOPT_URL,$url);
? ? ? ? ? ?curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
? ? ? ? ? ? ? curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳過證書檢查 ?
? ? ? ? ? curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);?
? ? ? ? ? ?if($type == 'post'){
? ? ? ? ? ? ? ?curl_setopt($ch,CURLOPT_POST,1);
? ? ? ? ? ? ? ?curl_setopt($ch,CURLOPT_POSTFIELDS,$arr);
? ? ? ? ? ?}
? ? ? ? ? ?//3.采集
? ? ? ? ? ?$output =curl_exec($ch);
? ? ? ? ? ?echo (curl_error($ch));
? ? ? ? ? ?//4.關(guān)閉
? ? ? ? ? ?curl_close($ch);
? ? ? ? ? ?if($res=='json'){
? ? ? ? ? ? ? ?if(curl_error($ch)){
? ? ? ? ? ? ? ? ? ?//請求失敗,返回錯(cuò)誤信息
? ? ? ? ? ? ? ? ? ?return curl_error($ch);
? ? ? ? ? ? ? ?}else{
? ? ? ? ? ? ? ? ? ?//請求成功,返回錯(cuò)誤信息
? ? ? ? ? ? ? ? ? ?return json_decode($output,true);
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ? ? ?echo var_dump( $output );
? ? ? ?}
public ? ? ? function getWxAccessToken(){
? ? ? ? ? ? ? ?//1.請求url地址
? ? ? ? $appid = 'wxd1157c516b3578d8';
? ? ? ? $appsecret = ?'26afef5b19e1ebfc3382604908460aa9';
? ? ? ? $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
? ? ? ? ? ? ? ?//2初始化
? ? ? ? $ch = curl_init();
? ? ? ? ? ? ? ?//3.設(shè)置參數(shù)
? ? ? ? curl_setopt($ch , CURLOPT_URL, $url);
? ? ? ? curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
? ? ? ? ? ? ? ? ?curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳過證書檢查 ?
? ? ? ? ? ? ? ? ?curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);?
? ? ? ? ? ? ? ?//4.調(diào)用接口?
? ? ? ? ? ? ? ? ?$res = curl_exec($ch);
? ? ? ? ? ? ? ?//5.關(guān)閉curl
? ? ? ? ? ? ? ? ?echo $res ;
? ? ? ? ? ? ? ? ?curl_close( $ch );
? ? ? ? ? ? ? ? ?if( curl_errno($ch) ){
? ? ? ? ? ? ? ? ? ? var_dump( curl_error($ch) );
? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ?$arr = json_decode($res, true);
? ? ? ? ? ? ? ?var_dump($arr["access_token"]);
? ? ? ? ? ? ? ? ?return $arr["access_token"];
? ? ? ? ? ? ?}
}
HTML代碼
<!doctype html>
<html>
<head>
? ? <title>微信js分享接口</title>
? ? <meta name="viewpoint" content = "initial-scale=1.0;width=device-width"/>
? ? <meta http-equiv="content" content = "text/html;charset=utf-8"/>
? ? <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
? ? <uses-permission android:name="android.permission.INTERNET"/>
</head>
<body>
? ?
? ?<script>
? ?wx.config({
? ?
? ? debug: true, ?
? ? ? ? appId: 'wxd1157c516b3578d8', // 必填,公眾號的唯一標(biāo)識
? ? ? ? timestamp:'<{$timestamp}>' , // 必填,生成簽名的時(shí)間戳
? ? ? ? nonceStr: '<{$nonceStr}>', // 必填,生成簽名的隨機(jī)串
? ? ? ? signature: '<{$signature}>',// 必填,簽名,見附錄1
? ? ? ? jsApiList: [
? ? ? ? "onMenuShareTimeline",
? ? ? ? "onMenuShareAppMessage"
? ? ? ? ] // 必填,需要使用的JS接口列表,所有JS接口列表見附錄2
? ? });
? ?wx.ready(function(){
? ? ? ? //分享到朋友圈
? ? ? ? alert(location.href.split('#')[0]);
? ? ? ? wx.onMenuShareTimeline({
? ? ? ? ? ?title: 'test1', // 分享標(biāo)題
? ? ? link: 'http://www.baidu.com', // 分享鏈接
? ? imgUrl: 'https://www.baidu.com/img/bd_logo1.png', // 分享圖標(biāo)
? ? success: function () {?
? ? ? ? // 用戶確認(rèn)分享后執(zhí)行的回調(diào)函數(shù)
? ? ? ? alert("success");
? ? },
? ? cancel: function () {?
? ? ? ? // 用戶取消分享后執(zhí)行的回調(diào)函數(shù)
? ? ? ? alert("cancle");
? ? }
});
? ? //分享給朋友
? ? wx.onMenuShareAppMessage({
? ? title: 'test1', // 分享標(biāo)題
? ? desc: 'test xjy', // 分享描述
? ? link: 'http://www.baidu.com', // 分享鏈接
? ? imgUrl: 'https://www.baidu.com/img/bd_logo1.png', // 分享圖標(biāo)
? ? type: 'link', // 分享類型,music、video或link,不填默認(rèn)為link
? ? dataUrl: '', // 如果type是music或video,則要提供數(shù)據(jù)鏈接,默認(rèn)為空
? ? success: function () {?
? ? ? ? // 用戶確認(rèn)分享后執(zhí)行的回調(diào)函數(shù)
? ? },
? ? cancel: function () {?
? ? ? ? // 用戶取消分享后執(zhí)行的回調(diào)函數(shù)
? ? }
});
});
? ?wx.error(function(res){
? ? // config信息驗(yàn)證失敗會執(zhí)行error函數(shù),如簽名過期導(dǎo)致驗(yàn)證失敗,具體錯(cuò)誤信息可以打開config的debug模式查看,也可以在返回的res參數(shù)中查看,對于SPA可以在這里更新簽名。
? ? alert("shibai");
});
? ?</script>
</body>
</html>
2016-12-28
要用域名訪問你的是本地 ip 吧