微信开发入门-2

接上一篇《微信开发入门-1》

上一篇介绍了官方后台的一些功能,没有用到开发者模式,下面进入开发者模式

开发者原理图解

20140714150416771.jpg

开发者服务器

要有一台自己能控制的服务器和域名,二级域名也可以

还是微信公众平台的官方后台,开发菜单
QQ图片20160309150716.png

基本配置
QQ图片20160309150752.png

微信给每一个公众号都分配了一个AppID(应用ID)
通过AppSecret(应用密钥)来实现加密通信,权限控制等等

要查看AppSecret(应用密钥),需要开启微信保护,按照提示得到一串密钥
类似bcc3f1321df5793a239e4dc813bb1c95
把AppID,AppSecret都记录下来,应用程序中将会用到

服务器配置

点击“修改配置”按钮,填写服务器地址(URL)、Token和EncodingAESKey,其中URL是开发者用来接收微信消息和事件的接口URL。Token可由开发者可以任意填写,用作生成签名(该Token会和接口URL中包含的Token进行比对,从而验证安全性)。EncodingAESKey由开发者手动填写或随机生成,将用作消息体加解密密钥。

消息加解密方式
明文模式下,不使用消息体加解密功能,安全系数较低
兼容模式下,明文、密文将共存,方便开发者调试和维护
安全模式下,消息包为纯密文,需要开发者加密和解密,安全系数高
如果选择了兼容模式或者安全模式,就需要用到上述的EncodingAESKey

服务器地址(URL)
对于开启了开发者模式的公众号,所有用户发来的微信消息和事件,都会以特定的XML格式POST到这个URL,我们就可以根据接收到的XML数据,做相应的操作处理,然后再以特定的XML格式的数据返回给微信服务器,实现与用户的互动。
另请注意,微信公众号接口只支持80接口。

接下来进入实例阶段,因本人所用PHP语言,故以PHP语言为例,但微信开发本身并不要求语言,只要按照规定的XML格式,走通微信接口,任何语言都可以进行微信公众号开发。

实例可以参考:
微信开发(PHP)初探-1
微信开发(PHP)初探-2

下面以本人新申请的个人订阅号为例:
qrcode_for_gh_6ac203bd7c5b_344.jpg

现有域名www.keyunq.com,在根目录下,新建weixin文件夹,新建index.php用来与微信服务器通讯
服务器地址(URL)就是http://www.keyunq.com/weixin/index.php

回到微信官方后台,服务器配置上

修改配置
URL: http://www.keyunq.com/weixin/index.php
TOKEN: (自己设定)keyunq
EncodingAESKey:(可随机生成)yqjrI2GNrjLJHyzA6VQhNNdDNMcDRrXq48wH1YDNw55
消息加解密方式: 先选明文模式

点提交,报TOKEN验证失败

需要先编辑index.php文件:

<?php
/**
  * wechat php test
  */

//define your token
define("TOKEN", "keyunq");
$wechatObj = new wechatCallbackapiTest();
if(!isset($_GET["echostr"])) {
    if($wechatObj->checkSignature()) {
        $wechatObj->responseMsg();
    }
} else {
    $wechatObj->valid();
}


class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }

    public function responseMsg()
    {
        //get post data, May be due to the different environments
        //接收POST过来的XML数据
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

        //extract post data
        if (!empty($postStr)){
                /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
                   the best way is to check the validity of xml by yourself */
                libxml_disable_entity_loader(true);
                //解析XML,把 XML 字符串载入对象中
                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();
                $textTpl = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <Content><![CDATA[%s]]></Content>
                            <FuncFlag>0</FuncFlag>
                            </xml>";             
                if(!empty( $keyword ))
                {
                    $msgType = "text";
                    //回复内容
                    $contentStr = "Welcome to keyunq's world!!!";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                    echo $resultStr;
                }else{
                    echo "Input something...";
                }

        }else {
            echo "";
            exit;
        }
    }

    public function checkSignature()
    {
        // you must define TOKEN by yourself
        if (!defined("TOKEN")) {
            throw new Exception('TOKEN is not defined!');
        }

        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        // use SORT_STRING rule
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
}

?>

将index.php上传到服务器上对应目录下,然后后台点击提交,没有问题的话,则通过验证。
然后点击“启用”服务器配置,用户消息和开发者需要的事件推送,都会被转发到该URL中

测试一下
IMG_8460.jpg

再测试一个生成菜单的接口
wiki:http://mp.weixin.qq.com/wiki/10/0234e39a2025342c17a7d23595c6b40a.html

接口调用请求说明
http请求方式:POST(请使用https协议) https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN

access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。开发者需要进行妥善保存。access_token的存储至少要保留512个字符空间。access_token的有效期目前为2个小时,需定时刷新,重复获取将导致上次获取的access_token失效。

所以调用接口前,我们先要把access_token保存下来,用来调用各种接口。

新建一个token.php文件

<?php

$appid = "wxad142631a1240e1b";
$secret = "9cf2780ea0db8401d8151448ddb381f6";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$rs = curl_exec($ch);
$data = json_decode($rs);

var_dump($data);
?>

返回值:
object(stdClass)#1 (2) { [“access_token”]=> string(107) “sNqfCXh9rtLhoTiapfArrCErcSxhUhvE9-flJOZ3Sfdi59I3xKMmJ70OZbwAQrqsB6TzQ80wFZu3jeHV1lsk0_iAEcP4W01OXGAWXacCS6s” [“expires_in”]=> int(7200) }

access_token 获取到的凭证
expires_in 凭证有效时间,单位:秒
可以看到有效时间为7200秒,因为接口调用每天有限制,不能每次都去调用,产生一个新的access_token,所以将access_token保存起来,判断时间是否过期,如过期就重新再取一次。
如下代码,将接口返回的json数据存进tokenfile,然后通过tokenfile文件的修改时间,判断是否过期

<?php
$m_time = filemtime("tokenfile");
$n_time = time();
$fs = file_get_contents("tokenfile");
$data = json_decode($fs);
$expires_in = intval($data->expires_in);

if($n_time - $m_time > $expires_in) {

    $appid = "wxad142631a1240e1b";
    $secret = "9cf2780ea0db8401d8151448ddb381f6";
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $rs = curl_exec($ch);

    file_put_contents("tokenfile",$rs);

    $c_data = json_decode($rs);

    $access_token = $c_data->access_token;

} else {
    $access_token = $data->access_token;
}

?>

在调用时只需加上include(“token.php”),即可在程序中使用$access_token 调用access_token。

得到$access_token后,就可以通过接口生成菜单了。

综合一下,写一个微信的操作class,保存为class/WxAction.class.php文件,代码如下

<?php
class WxAction {

    public function __construct() {

    }

    //取全局凭证access_token
    public function getAccessToken($appid,$secret) {
        $m_time = filemtime("tokenfile");
        $n_time = time();
        $fs = file_get_contents("tokenfile");
        $data = json_decode($fs);
        $expires_in = intval($data->expires_in);

        if($n_time - $m_time > $expires_in) {

            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";

            $rs = $this->doCurlGetRequest($url);

            file_put_contents("tokenfile",$rs);

            $c_data = json_decode($rs);

            $access_token = $c_data->access_token;

        } else {
            $access_token = $data->access_token;
        }
        return $access_token;
    }

    //curl调用微信https接口-get方式
    public function doCurlGetRequest($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    //curl调用微信https接口-post方式
    function doCurlPostRequest($url, $jsonData){
        $con = curl_init((string)$url);
        curl_setopt($con, CURLOPT_HEADER, false);
        curl_setopt($con, CURLOPT_POSTFIELDS,$jsonData);
        curl_setopt($con, CURLOPT_POST, true);
        curl_setopt($con, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($con, CURLOPT_SSL_VERIFYPEER,false);  //略过证书验证
        $result = curl_exec($con) ;
        if(curl_errno($con))
        {
          file_put_contents("tmp.txt", curl_errno($con).PHP_EOL,FILE_APPEND);
        }

        return $result;
    }


    //生成自定义菜单
    public function createMenu($access_token,$menu_arr) {


        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;

        $data = $menu_arr;

        $rs = $this->doCurlPostRequest($url,$data);
        return $rs;

    }

    //取用户信息
    public function getUserInfo($access_token,$openid,$lang) {

        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$access_token}&openid={$openid}&lang={$lang}";

        $rs = $this->doCurlGetRequest($url);
        return $rs;

    }

    //验证微信加密签名
    public function checkSignature()
    {
        // you must define TOKEN by yourself
        if (!defined("TOKEN")) {
            throw new Exception('TOKEN is not defined!');
        }

        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
             // use SORT_STRING rule
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
}

生成菜单的代码 menu.php:

<?php
$appid = "wxad142631a1240e1b";
$secret = "9cf2780ea0db8401d8151448ddb381f6";
include("class/WxAction.class.php");
$wx = new wxAction();

$menu_arr ='{
    "button":[
    {
        "type":"view",
        "name":"博客",
        "url":"http://www.keyunq.com/"
    },
    {
        "name":"各种菜单",
        "sub_button":[
        {
            "type":"scancode_waitmsg",
            "name":"扫码带提示",
            "key": "rselfmenu_0_0", 
                            "sub_button": [ ]
        },
        {
            "type":"pic_photo_or_album",
            "name":"拍照或者相册发图",
            "key": "rselfmenu_1_1", 
                            "sub_button": [ ]
        },
        {
            "type":"location_select",
            "name":"发送位置",
            "key": "rselfmenu_2_0"
        }]
    },
    {
        "name":"便民服务",
        "sub_button":[
        {   
            "type":"view",
            "name":"附近油站",
            "url": "http://m.amap.com/around/?locations=&keywords=%E4%B8%AD%E7%9F%B3%E5%8C%96%E5%8A%A0%E6%B2%B9%E7%AB%99&defaultIndex=1&defaultView=map&searchRadius=50000&key=9fbdb2faec1ff65191b0106592626a4e"
        }]
    }]
 }';

$access_token = $wx->getAccessToken($appid,$secret);

$rs = $wx->createMenu($access_token,$menu_arr);
echo $rs;

浏览器访问一下 http://www.keyunq.com/weixin/menu.php
发现报错:{“errcode”:48001,”errmsg”:”api unauthorized hint: [0Q1370641vr23]”}

原因是 未认证的个人订阅号 并没有自定义菜单的接口权限

于是将
appid=wx3e07c141aa46e2be; secret = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”;

修改成接口测试号的appid和appsecret

接口测试号是微信开放的专用于测试的,拥有所有高级权限,每个人都可以申请一个接口测试号

申请地址:http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

修改配置:
同订阅号类似,填写url和token
QQ图片20160310110546.png

然后关注该测试号
0.jpg

将menu.php里的 appid secret换成测试号的
浏览器访问一下 http://www.keyunq.com/weixin/menu.php
还是报错
因为tokenfile保存的还是之前订阅号的信息,删除tokenfile文件,重试
{“errcode”:0,”errmsg”:”ok”}
证明生成成功

进入测试号看一下

IMG_8461.jpg

可以看到,菜单已经建好。index.php里的自动回复,也生效了。

通过2个不同的微信号的切换,可以看到,只要修改微信公众号的url,token
程序里的appid和appsecret,一套程序可以用在不同的公众号里。

从而理解文首的那张原理图

用户-》微信服务器-》(URL,TOKEN)-》开发者服务器
用户《-微信服务器《-(appid,appsecret)《-开发者服务器

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值