微信开发文档php

第一步:基本配置

设置url(为资源服务器,可以进行token验证和返回token的一个地址下)。

Token为返回的一个token

EncodingAESKey:随机生成。

第二步:Appid和Appsecret设置和生成。

获取AppId和AppSecret:成为开发者后,在“开发者中心中”页面,可获取AppId和AppSecret

两个参数。

 

第三步:编写文件

在url路径中的文件中返回一个echostr。即可

若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,成为开发者成功(不过在此过程中需要进行signature对请求进行校验);可不进行校验

此GET请求中有四个参数:

signature:微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。

timestamp:时间戳

nonce:随机数

echostr:随机字符串.

)将token、timestamp、nonce三个参数进行字典序排序

2)将三个参数字符串拼接成一个字符串进行sha1加密

3)开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。

微信接入指南——wx_sample.php,可以返回文本信息。

define("TOKEN", "gewechatbtp");

$wechatObj = new wechatCallbackapiTest();

$wechatObj->valid();

$wechatObj->responseMsg();

 

class wechatCallbackapiTest

{

    public function valid()

    {

        $echoStr = $_GET["echostr"];

 

        //valid signature , option

        if($this->checkSignature()){

           echo $echoStr;

           exit;

        }

    }

 

    public function responseMsg()

    {

       $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

 

        //extract post data

       if (!empty($postStr)){

             

                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 = "<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 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;

       }

    }

}

当已经验证通过后,就进行使用下列的

 

自定菜单:

直接运行改路径:http://gewechat.ge.com.cn/gehelp/testplan/menu1.php

 

 

网页授权的信息

用户在微信端访问第三方的网页,

在微信界面中的第三方网页,可以通过《微信的网页授权过后》就是给一个回调域名也就是第三方网页的域名,获取用户信息。

经过授权过后,就可以获取用户的信息

如果是用于进入该网页的openidscope的静态默认授权base

如果是用于获取该用户信息,则需要用户同意授权给公众号,为userinfo的授权。

 

         微信网页授权是通过OAuth2.0机制实现

         urlencode('http://gewechat.ge.com.cn/gehelp/tokenopen.php');

         http%3A%2F%2Fgewechat.ge.com.cn%2Fgehelp%2Ftokenopen.php\

         授权后的accesstoken     -----IUCLnCh0K7-SwE1BSObpRrFma-FNsoILt3JMOJ3UHGKmorMlMcpeafdldvU3M1Qa4V8WJqIZXrVHwLMGCd6DAuRzSVeAhScgxv3pmznyGa0

         用户授权给公众号,会获取到一个特有的网页授权的accesstoken(特有的接口调用凭证)

         https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxafed5daec5091a25&redirect_uri=.urlencode().&response_type=code&scope=snsapi_userinfo&state=wxbase#wechat_redirect

         redirect_uri?code=CODE&state=STATE可以获取code

         需要通过code来换取

                   https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxafed5daec5091a25&redirect_uri=http%3A%2F%2Fgewechat.ge.com.cn%2Fgehelp%2Ftokenopen.php&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect

                   redirect_uri?code=CODE&state=STATE可以获取code(GET里面,)

                  

                   https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxafed5daec5091a25&secret=571613c41cc46a56b4f89c9e26227a45&code=9118&grant_type=authorization_code

                   授权后的accesstoken     -----IUCLnCh0K7-SwE1BSObpRrFma-FNsoILt3JMOJ3UHGKmorMlMcpeafdldvU3M1Qa4V8WJqIZXrVHwLMGCd6DAuRzSVeAhScgxv3pmznyGa0

        

         网页授权的access_token可以进行授权后接口调用。

其他微信接口,需要通过基础支持中的“获取access_token”接口来获取到的普通access_token调用https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

检验授权凭证(access_token)是否有效

httpGET(请使用https协议) https://api.weixin.qq.com/sns/auth?access_token=ACCESS_TOKEN&openid=OPENID

{ "errcode":0,"errmsg":"ok"}

错误时的JSON返回示例:

{ "errcode":40003,"errmsg":"invalid openid"}

 

推送事件:

 

 

 

 

 

 

网页授权

 

直接通过获取codeGET请求,然后如果不为空,则获取授权token链接

"https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";

回调域名需要配置为gewechat.ge.com.cn

如果为空

$callback=urlencode("http://gewechat.ge.com.cn/gehelp/testplan/person_info.php");

回调的域名的里面含有code

$codeurl="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$callback."&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect";

 

Header(“Location: ”. $codeurl);

 

 

用户信息展示

<?php

header("Content-type:text/html;charset=utf-8");

 

 

$token_message= new Tokeninfomation();

$token =$token_message->gettoken();

$result=$token_message->getall_openid($token);

class Tokeninfomation{

    public function gettoken(){

        $appid="wx8350bd9db3b05ef3";

        $appsecret="50c1ade81b8bc5b5bbcb15af33af4a64";

        // "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx8350bd9db3b05ef3&secret=50c1ade81b8bc5b5bbcb15af33af4a64";

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

        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        $output = curl_exec($curl);

        curl_close($curl);

        $result = json_decode($output, true);

 

        $access_token = $result["access_token"];

        return $access_token;

 

    }

    public function getall_openid($token){

        $url ="https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$token."&next_openid=";

        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        $output = curl_exec($curl);

        curl_close($curl);

        $result = json_decode($output, true);

 

        $data= $result["data"];

        return $data['openid'];

    }

    public function getone_info($token,$openid){

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

        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        $output = curl_exec($curl);

        curl_close($curl);

        $result = json_decode($output, true);

 

        return $result;

    }

}

?>

<html>

    <head>

        <title>通讯录</title>

    </head>

   

    <body style="background:#B7B7B7;">

    <div style="width:90%;margin:auto 5%">

      

       <div style="margin:10px;background-color:#5F9EA0;border-radius:10px;border-bottom: #cecdcd 2px dashed;">

           <div align="left" style="width:90%;height:10%">

               <p>&nbsp&nbsp&nbsp<input type="text" style="width: 95%;height:90%;border-radius:10px;" id="search" ></p>

           </div>

       </div>

       <?php

            for($i=0;$i<count($result);$i++){

                $oneinfo=$token_message->getone_info($token, $result[$i]);

                echo "<br>";

           

            ?>

 

       <a href="#"><div align="left" style="margin:10px;background-color:#4682B4;overflow:hidden;border-radius:10px;">

           <div style="width:25%;height:200px;float:left">

              <img src="<?php echo $oneinfo['headimgurl']?>" style="width: 60%;height:70%;margin:15% 10%;border-radius:5px;">

           </div>

               <input type="hidden" id="openid" value="<?php echo $oneinfo['openid']?>">

           <div style="margin:auto 1%;width:73%;height:200px;float:right;border-radius:10px; line-height:200px;">

              <span id="username" ><font size=6><?php echo $oneinfo['nickname']?></font></span>

           </div>

         </div></a>

            <?php

            }

            ?>

       <div align="center" style="margin:10px;height:100px;background-color:#698B22;overflow:hidden;border-radius:10px;line-height:100px;">

             <font size=7 >已经是最底了!</font>

       </div>

    </div>

</body>

</html>

 

 

 

 

 

phpWechat 是由一个具有多年行业开发经验的前端 UI 设计师、PHP开发工程师组成的团队设计、研发的一套的微信公众平台管理系统,您可以瞬间完成一个公共号或者PC站或者两者皆有的平台搭建。 phpWeChat 微信+网站开发框架功能亮点: 1、100%开源,没有任何加密文件 phpWeChat核心文件100%开源,没有任何加密文件,开发者可以放心使用而无需担心留有后门程序等。 2、程序轻量级,一天读完所有代码 核心框架压缩包只有2.66M,十分利于开发者迅速阅读全部代码。 3、代码严谨,结构清晰 phpWeChat使用MVC开发模式,各个功能模块之间独立并目录结构统一。开发者可迅速掌握phpWeChat的框架结构。 4、二次开发文档十分完善 我们提供了完善的phpWeChat二次开发文档,便于开发者学习与查阅。 5、高度集成微信接口 phpWeChat高度集成了微信公共号的自动回复、菜单管理、素材管理、模板消息、粉丝管理、微信支付等常用接口,您只需一个函数或2/3行代码即可实现原本需要很费时费力才能开发的功能。 6、phpWeChat也是一个PC(网站)开发框架和CMS 慢慢您会发现,phpWeChat是一个更接近CMS功能的框架。 7、高度封装常用开发功能 只需一两行固定的代码,您便可以写出列表分页、微信支付、文件上传、邮件发送、短信发送等功能。 8、数据结构合理,负载强劲 phpWeChat集成了常见的内存级缓存(MemCache)、文件缓存处理方案,使得系统更符合大数据、大并发的公共号或网站使用。 9、集成应用市场,功能拓展一瞬间 phpWeChat集成了应用市场并在线安装应用的功能,您可以在一瞬间安装完成其他开发开发的功能模块。 phpWeChat安装方法: 将下载的程序压缩包(zip格式)解压后,将全部文件和文件夹上传至网站根目录。然后输入域名/install.php 即可进入安装程序。 注意:安装phpWeChat前请先创建好phpWeChat所需的数据库。 phpWeChat 目录结构: addons 功能模块目录 admin 公用后台目录 api 插件目录 data 数据缓存目录 install 安装程序目录,安装后请改名或删除 include 公用操作类目录 statics 静态文件目录 template 视图(模板)目录 upload 上传文件存放目录 phpWeChat 更新日志: 新版v1.0.3发布: phpWeChat PC+微信公众号开发核心框架v1.0.3针对v1.0.2版本出现的问题,主要修复了和增加了以下功能: 1、优化自定义模块操作; 2、优化安装过程; 3、修复一处在线支付的PHP版本兼容问题; 4、修复一处自定义模块的问题; 5、修复后台模块管理显示未知版本的bug; 6、修复粉丝行为IP报错的Bug; 7、删除一些冗余代码; 8、修复消息模板不存在时,一直提示[同步中]的bug。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值