微信分享接口 JS-SDK 使用小结

李鹏2 5,753 次浏览

微信开放了分享接口 js-sdk

在使用的过程中,得注意下以下两个地方,不然你会陷入大坑

1.你使用的微信版本,如果是安卓手机,请确保你的微信版本在6.0.2.58及以上版本。

2.你需要到微信公众平台去设置“JS接口安全域名”, 设置方式:公众号设置 ->  功能设置 -> JS接口安全域名

使用方法:

1.在需要调用JS接口的页面引入如下JS文件.

2.通过config接口注入权限验证配置

3.通过ready接口处理成功验证

4.通过error接口处理失败验证

 

在config接口注入权限验证配置时,php可以这么处理

1.下载微信公众平台提供的 示例代码,下载地址:http://demo.open.weixin.qq.com/jssdk/sample.zip

2.解压sample.zip,提取里面php目录的jssdk.php ,然后在你处理的php程序里面引入jssdk.php 文件,你可以根据情况去修改jssdk.php ,另外值得注意的是,请你最好做好accesstoken以及jsapi_ticket的缓存,以确保不会触发频率限制。

下面的代码是我在Thinkphp框架里面写的测试文件,如果非Thinkphp的开发者,请使用include进行导入。

 public function share(){
        import("@.Jssdk.Jssdk","",".php");
        $jssdk = new JSSDK(C('API'),C('AppSecret'));
        $signPackage = $jssdk->GetSignPackage();
        $this->assign('signPackage',$signPackage);
        $this->display();
    }

其他的配置我就不需要多说什么了,你可以参考微信JSSDK说明文档 进行参考开发。

微信JSSDK说明文档地址:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html

 

下面是jssdk.php文件代码

<?php
/**
 * Created by PhpStorm.
 * User: lipeng
 * Date: 15-1-12
 * Time: 上午10:15
 */
class JSSDK {
    private $appId;
    private $appSecret;

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

    public function getSignPackage() {
        $jsapiTicket = $this->getJsApiTicket();
        $url = "http://$_SERVER[HTTP_HOST]$_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("./Main/Lib/Jssdk/jsapi_ticket.json"));
        if ($data->expire_time < time()) {
            $accessToken = $this->getAccessToken();
            $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$accessToken}&type=jsapi";
            $res = json_decode($this->httpGet($url));
            $ticket = $res->ticket;

            if ($ticket) {
                $data->expire_time = time() + 7000;
                $data->jsapi_ticket = $ticket;
                $fp = fopen("./Main/Lib/Jssdk/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("./Main/Lib/Jssdk/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 = json_decode($this->httpGet($url));
            $access_token = $res->access_token;
            if ($access_token) {
                $data->expire_time = time() + 7000;
                $data->access_token = $access_token;
                $fp = fopen("./Main/Lib/JSSDK/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_URL, $url);

        $res = curl_exec($curl);
        curl_close($curl);

        return $res;
    }
}

 

 

 
微信开放平台回复的邮件:
 
 
 
 

2 thoughts on “ 微信分享接口 JS-SDK 使用小结 ”

  1. 化学试剂

    是不是xml数据交换

  2. 电缆支架

    技术大拿呀

发表评论

? razz sad evil ! smile oops grin eek shock ??? cool lol mad twisted roll wink idea arrow neutral cry mrgreen