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');
原文地址:https://www.cnblogs.com/justsus/p/14042804.html