首页 新闻 会员 周边 捐助

做微信分享网页分享链接的简介和缩略图的修改提示signature签名无效

0
悬赏园豆:20 [已解决问题] 解决于 2020-09-03 16:56

大家好,我最近在做一个微信分享网页分享链接的简介和缩略图的修改功能,按着文档和网上的教程来搞了一遍下来,提示签名无效。之后做了很多次各种检查都没发现问题在哪?求指点,下面是js代码,公众号那边域名都绑好的了。
<script src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
<script>
$(function() {
var baseurl = location.href.split('#')[0];
// var baseurl = location.href;

    if(baseurl.search("html")==-1){
        baseurl=baseurl+"index.html";
    }
    baseurl=encodeURI(baseurl);
    // <span>alert</span>(baseurl);
    $.ajax({
        url :"http://www.隐藏/wx_token.php?"+baseurl,
        // type:'get',
        dataType : "json",
        timeout : 5000,
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            if (textStatus == "timeout") { // 请求超时
                <span>alert</span>("请求超时");
            } else {
                <span>alert</span>(errorThrown);
            }
        },
        
        success : function(data, textStatus) {
            if (textStatus == "success") { // 请求成功
                // <span>alert</span>(JSON.stringify(data));
                // <span>alert</span>("1:"+data.ticket);
                // <span>alert</span>("2:"+data.nonceStr);
                // <span>alert</span>("3:"+data.timestamp);
                // <span>alert</span>("4:"+data.url);
                // <span>alert</span>("5:"+data.signature);
                // <span>alert</span>("6:"+data.token); 
                wx.config({
                    debug : true,
                    appId : '隐藏',
                    timestamp : data.timestamp,
                    nonceStr : data.nonceStr,
                    signature : data.signature,
                    jsApiList : [ 'updateAppMessageShareData','updateTimelineShareData','onMenuShareWeibo']
                });
                wx.ready(function(){

                    <span>alert</span>('初始化成功');
                });
                wx.error(function(res){

                  <span>alert</span>('初始化失败');
                  
                  
                  
                });
                
            }
            
        }
    });
});
</script>

以下是PHP代码

<?php
//启动

getSignPackage();

//1获取accesstoken
function getAccessToken()
{

     $data = json_decode(file_get_contents("access_token.json"));
  if ($data->expire_time < time()) {
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=隐藏&secret=隐藏";
    $res = json_decode(curlHttp($url));
    $access_token = $res->access_token;
    if ($access_token) {
      $data->expire_time = time() + 7000;
      $data->access_token = $access_token;
      $fp = fopen("access_token.json", "w");
      fwrite($fp, json_encode($data));
      fclose($fp);
    }
  } else {
    $access_token = $data->access_token;
  }
  return $access_token;

    
    
}

 function curlHttp($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
    curl_setopt($curl, CURLOPT_TIMEOUT, 500 );
    curl_setopt($curl, CURLOPT_URL, $url );
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,false);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
}

//2.获取api_ticket
function getJsApiTicket($accessToken) {
$data = json_decode(file_get_contents("jsapi_ticket.json"));
if ($data->expire_time < time()) {
// $accessToken = getAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
$res = json_decode(curlHttp($url));
$ticket = $res->ticket;
if ($ticket) {
$data->expire_time = time() + 7000;
$data->jsapi_ticket = $ticket;
$fp = fopen("jsapi_ticket.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
} else {
$ticket = $data->jsapi_ticket;
}
return $ticket;

}

// 3获取签名
function getSignPackage() {
// 获取token
$token = getAccessToken();
// 获取ticket
$ticket = getJsApiTicket($token);
// $ticket = $ticketList['ticket'];

    // 该url为调用jssdk接口的url
    $url = urldecode($_SERVER["QUERY_STRING"]);
    
    // 生成时间戳
    $timestamp = time();
    // 生成随机字符串
    $nonceStr = createNoncestr();
    // 这里参数的顺序要按照 key 值 ASCII 码升序排序 j -> n -> t -> u
    $string = "jsapi_ticket=$ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
    $signature = sha1($string);
    $signPackage = array (
        "appId" => '隐藏',
        "nonceStr" => $nonceStr,
        "timestamp" => $timestamp,
        "url" => $url,
        "signature" => $signature,
        "rawString" => $string,
        "ticket" => $ticket,
        "token" => $token
    );
    // var_dump(json_encode($signPackage));
    // 返回数据给前端
    echo json_encode($signPackage);
}

// 创建随机字符串
 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;
}
问题补充:

很急,能解决的可以给顿饭钱,别嫌少啊我很穷的

灵沐一修闲的主页 灵沐一修闲 | 菜鸟二级 | 园豆:345
提问于:2020-08-01 16:24
< >
分享
最佳答案
0

// 这里参数的顺序要按照 key 值 ASCII 码升序排序 j -> n -> t -> u
$string = "jsapi_ticket=$ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url";

$string这里面的“xtamp”写错了,改回&timestamp,

灵沐一修闲 | 菜鸟二级 |园豆:345 | 2020-09-03 16:47

哈哈哈~~~ 感谢园豆。但是这个问题也太搞笑了。

泰乐世陶潜 | 园豆:432 (菜鸟二级) | 2020-09-03 17:14
其他回答(1)
0

if ($data->expire_time < time()) {
// $accessToken = getAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";

这里我感觉有问题吧,你的$accessToken我理解应该是一个变量,但是你现在这里赋值了吗?你不是注释掉了吗?

收获园豆:20
泰乐世陶潜 | 园豆:432 (菜鸟二级) | 2020-08-02 23:40

$accessToken通过函数function getJsApiTicket($accessToken) 传参数过来,直接获取放到生成ticket链接上的

支持(0) 反对(0) 灵沐一修闲 | 园豆:345 (菜鸟二级) | 2020-08-03 09:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册