鍍金池/ 問(wèn)答/PHP  網(wǎng)絡(luò)安全  HTML/ 請(qǐng)問(wèn)為什么我用JSSDK做微信分享每點(diǎn)擊個(gè)四五次以后就會(huì)報(bào)錯(cuò)呢?

請(qǐng)問(wèn)為什么我用JSSDK做微信分享每點(diǎn)擊個(gè)四五次以后就會(huì)報(bào)錯(cuò)呢?

<?php
/**
 * Created by PhpStorm.
 * User: Artist
 * Date: 2017/11/6
 * Time: 10:24
 */

namespace APP\Common;


class JSSDK
{
    private $appId;
    private $appSecret;

    public function __construct($appId, $appSecret) {
        $this->appId = $appId;
        $this->appSecret = $appSecret;
    }

    public function getSignPackage() {
        $jsapiTicket = $this->getJsApiTicket();

        // 注意 URL 一定要?jiǎng)討B(tài)獲取,不能 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();

        // 這里參數(shù)的順序要按照 key 值 ASCII 碼升序排序
        $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$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;
    }

    private function getJsApiTicket() {

        $accessToken = $this->getAccessToken();

        // 如果是企業(yè)號(hào)用以下 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));

//下面一句是65行

        $ticket = $res->ticket;
        return $ticket;
    }

    private function getAccessToken() {
        $data = json_decode(file_get_contents(public_path("access_token.json")));//建立一個(gè)文件把第一次請(qǐng)求到的token對(duì)象放進(jìn)去
        if (empty($data) || $data->expire_time < time()) {
            $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));
            $access_token = $res->access_token;
            $expires_in = $res->expires_in;
            if ( $access_token ) {
                $data->expire_time = time() + ($expires_in - 100);
                $data->access_token = $access_token;
                $fp = fopen(public_path("access_token.json"), "w");
                fwrite($fp, json_encode($data));
                fclose($fp);
            }
        } 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);
        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;
    }
}

圖片描述

64行$res正確時(shí)內(nèi)容

clipboard.png

64行$res錯(cuò)誤時(shí)內(nèi)容
clipboard.png

我的access_token.json文件是放在public下面的,里面是有內(nèi)容寫(xiě)入的。
使用個(gè)四五次以后就會(huì)報(bào)錯(cuò)。
請(qǐng)大佬教我。

回答
編輯回答
誮惜顏

access_token失效了,應(yīng)該是你別的地方調(diào)用了access_token,而文件沒(méi)有更新?

2018年2月20日 14:03
編輯回答
近義詞

把64行那個(gè)$res打出來(lái), 看內(nèi)容.

2017年4月23日 20:22