天天看點

微信分享連結或網站文章到微信朋友圈,縮略圖檔不顯示 1.微信分享 "Reference error wx is not defined"錯誤

可用代碼

html頁面

```

<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>

<script>

window.οnlοad=function(){//進入頁面就執行ajax,目的為了傳送目前頁面url#前的完整url

var ajaxurl =  'Wechat.php';//注意路徑 

var urll = location.href.split('#')[0]; //頁面url#前的完整url,可alert彈出檢視

var data = {

    "urll":urll,

    random:Math.random()

};

$.ajax({

    url:ajaxurl,

    data:data,

    type:"POST",

    dataType:"JSON",

    success:function (ress){

        //成功則執行JS-SDK

        //console.log(ress);//檢視傳回結果

        //執行JS_SDK

        wx.config({

            debug: false, 

            appId: ress.appId,

            timestamp: ress.timestamp,

            nonceStr: ress.nonceStr, 

            signature: ress.signature,

            jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage'] 

        }); 

        },

});

}

 //alert(location.href.split('#')[0]);  //彈出的url必須與通路位址一緻

</script>

```

Wechat.php

```

<?php

$url = $_POST['urll'];

include('jssdk.php');

$jssdk = new JSSDK("你的appid", "你的appserectid");

$signPackage = $jssdk->GetSignPackage($url);

echo json_encode($signPackage);

```

jssdk.php

```

<?php

//error_reporting(0);

class JSSDK {

  private $appId;

  private $appSecret;

  public function __construct($appId, $appSecret) {

    $this->appId = $appId;

    $this->appSecret = $appSecret;

  }

  public function getSignPackage($theurl) {

    $jsapiTicket = $this->getJsApiTicket();

$url = $theurl;

    //$url = "http://$_SERVER[HTTP_NAME]$_SERVER[REQUEST_URI]";

    $timestamp = time();

    $nonceStr = $this->createNonceStr();

    // 這裡參數的順序要按照 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() {

    // jsapi_ticket 應該全局存儲與更新,以下代碼以寫入到檔案中做示例

    $data = json_decode(file_get_contents("jsapi_ticket.json"));

    if ($data->expire_time < time()) {

      $accessToken = $this->getAccessToken();

      $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";

      $res = json_decode($this->httpGet($url));

      var_dump($res);

      $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;

  }

  private function getAccessToken() {

    // access_token 應該全局存儲與更新,以下代碼以寫入到檔案中做示例

    $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=$this->appId&secret=$this->appSecret";

 //$res = file_get_contents($url);

      $res = json_decode($this->httpGet($url));

      var_dump($res);

      $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;

  }

  private function httpGet($url) {

    $curl = curl_init();

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($curl, CURLOPT_TIMEOUT, 500);

    curl_setopt($curl, CURLOPT_HEADER, 0);

    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;

  }

}

```

基本思路

1、公衆号。 2、微信認證。 3、域名備案。 4、JS接口安全域名設定。 5、開啟開發者模式,擷取appid、AppSecret。 6、ip白名單(伺服器的ip)。 7、伺服器配置開啟,填寫伺服器位址、 8.冷靜分析 錯誤原因 一味百度就去寫不思考誤終身

9.多謝我的組長!

參考連結

參考一問題就是ajax的data根本就沒有在php檔案裡使用 也就是麼有對目前url進行簽名  導緻一直簽名錯誤 ajax是post 是以在wechat.php裡使用Post擷取到了簽名

已解決問題“ 

1.微信分享 "Reference error wx is not defined"錯誤

解決辦法:<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>

2.“伺服器不發送請求”

解決辦法:一直報錯 後來在伺服器ping dns解析的一個Ip 根本就ping 不通  就改為可以ping通的一個位址

待解決問題“Access-Control-Allow-Origin”ajax跨域問題,加入<meta http-equiv="Access-Control-Allow-Origin" content="*">  這個也是不可以得,就在需要本接口的模闆目前路徑都複制粘貼了wechat.php和jsdk.php

繼續閱讀