• php开发微信公众号踩坑


    1. 环境部署
      服务器准备好,项目文件夹wechat,新建index.php文件,项目访问:http://www.domain.com/index.php

    如何验证:

    微信公众号开发文档有写:

    接受参数就是这图片中的几个。而且也给出了php版的验证函数。所以直接用就好了

    index.php代码:

    可以将微信服务的参数输出到日志中,方便查看。接受post参数类型为xml的可以使用 file_get_contents('php://input') 来接受

    <?php
    // >>>>>>>>>>>>>>>>>>>>>>>>>记录请求log<<<<<<<<<<<<<<<<<<<<<<<
    $date = date('Y-m-d H:i:s');
    $file = '0.log';
    $get = serialize($_GET);
    $get = $date . '<<<<<<<<<<<<<<<<<GET' . "
    " . $get . "
    ";
    file_put_contents($file, $get, FILE_APPEND);
    
    $post= serialize($_POST);
    $post= '<<<<<<<<<<<<<<<<<POST' . "
    " . $post. "
    ";
    file_put_contents($file, $post, FILE_APPEND);
    
    //$xml= serialize($GLOBALS['HTTP_RAW_POST_DATA']);  // 高版本php7不适合,请使用下面的
    $postxml = file_get_contents('php://input');
    $xml= serialize($postxml);
    $xml= '<<<<<<<<<<<<<<<<<XML' . "
    " . $xml. "
    ";
    file_put_contents($file, $xml, FILE_APPEND);
    
    // >>>>>>>>>>>>>>>>>>>>>>>>>记录请求log<<<<<<<<<<<<<<<<<<<<<<<
    
    define('TOKEN', '2020');
    $wechatObj = new wechatCallbackapiTest();
    // 校验
    if (!empty($_GET["echostr"])) {
        $wechatObj->valid();
    } else { //
        $wechatObj->responseMsg();
    }
    
    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 = "<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);
            sort($tmpArr, SORT_STRING);
            $tmpStr = implode($tmpArr);
            $tmpStr = sha1($tmpStr);
    
            if ($tmpStr == $signature) {
                return true;
            } else {
                return false;
            }
        }
    
    
    }
    
    
    

    新手坑:

    怎么给微信服务器返回数据:
    echo $echoStr;exit();
    怎么接受POST数据:
    file_get_contents('php://input');
    
  • 相关阅读:
    C++操作文件行(读取,删除,修改指定行)
    Windows注册表中修改UAC(用户账号控制)及批处理脚本
    Centos7.x 安装libevent2.x
    【Docker】:使用docker安装redis,挂载外部配置和数据
    【Docker】:使用docker安装mysql,挂载外部配置和数据
    开源定时任务框架Quartz(二)
    开源定时任务框架Quartz(一)
    Spring Boot系列教程十四:Spring boot同时支持HTTP和HTTPS
    数据结构与算法:单向链表实现与封装(有头)
    【C++札记】指针函数与函数指针
  • 原文地址:https://www.cnblogs.com/justsus/p/14042804.html
Copyright © 2020-2023  润新知