微信开发一

接入微信公众平台开发,开发者需要按照如下步骤完成:

1、填写服务器配置
2、验证服务器地址的有效性
3、依据接口文档实现业务逻辑

第一步:填写服务器配置

屏幕快照 2016-06-13 下午7.32.35.png

其中URL为验证消息的确来自微信服务器所存的PHP文件的路径;
Token为自己填写的任意字符,后面还会用到;

第二步:验证消息的确来自微信服务器

保存以下php代码并打包上传到服务器

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
        $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);
                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();
                $textTpl = "
                            
                            
                            %s
                            
                            
                            0
                            ";
                if(!empty( $keyword ))
                {
                    $msgType = "text";
                    $contentStr = "Welcome to wechat world!";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                    echo $resultStr;
                }else{
                    echo "Input something...";
                }

        }else {
            echo "";
            exit;
        }
    }

    private 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;
        }
    }
}

?>

第三步:依据接口文档实现业务逻辑

1.网页授权获取用户基本信息
(首先需要在权限列表中网页帐号->[网页授权获取用户基本信息]修改权限,填写自己的网站网址);

第一步:用户同意授权,获取code

(中括号内的为需要填写的自己的信息)(后面的访问也是这样访问)

https://open.weixin.qq.com/connect/oauth2/authorize?appid=[APPID]&redirect_uri=[REDIRECT_URI]&response_type=code&scope=[SCOPE]&state=STATE#wechat_redirect```
此时PHP代码则可以获取code

header("Content-type:text/html;charset=utf-8");
$code=$_GET['code'];
echo $code;


**第二步:通过code换取网页授权access_token**
获取code后,请求以下链接获取access_token: 
(中括号内的为需要填写的自己的信息)

https://api.weixin.qq.com/sns/oauth2/access_token?appid=[APPID]&secret=[SECRET]&code=[CODE]&grant_type=authorization_code



php代码示例

header("Content-type:text/html;charset=utf-8");
$code=$_GET['code'];
$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx743540570fe83a69&secret=d23fa1d3e81aeaa0a9b80e1829fd9642&code=".$code."&grant_type=authorization_code";

而此时获取查询结果可已通过查询

header("Content-type:text/html;charset=utf-8");
$code=$_GET['code'];
$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx743540570fe83a69&secret=d23fa1d3e81aeaa0a9b80e1829fd9642&code=".$code."&grant_type=authorization_code";
$res=json_decode(file_get_contents($url));
$access_token=$res->access_token;
$open=$res->openid;
$infourl="https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$open."&lang=zh_CN";
$infores=json_decode(file_get_contents($infourl));
var_dump($infores);
?>

目的:使用jssdk功能

使用流程(接入jssdk流程):

---1、设置安全域名

---2、引入js 文件

---3、注入config接口,进行验证

---4、验证成功,通过ready方法 回调jssdk功能。(具体实现什么,看需求)

重点:步骤三---验证

1、获取access_token

2、通过 access_token 获取jsapi_ticket

3、利用jsapi_ticket生成签名

4、将 签名、随机字符串、时间戳、string以数组形式返回。

你可能感兴趣的:(微信开发一)