博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
微信接口
阅读量:6946 次
发布时间:2019-06-27

本文共 2058 字,大约阅读时间需要 6 分钟。

<?php

/**
  * wechat php test
  */
//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$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
        $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);
        // use SORT_STRING rule
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
}
?>

bug 提示:

// $GLOBALS可能被禁用

//$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

-> new

 $postStr = file_get_contents("php://input");
 file_put_contents("log.txt",$postStr,FILE_APPEND );

转载于:https://www.cnblogs.com/simadongyang/p/8035389.html

你可能感兴趣的文章
最短路径
查看>>
手机评测
查看>>
java ssm 后台框架平台 项目源码 websocket 即时通讯 IM quartz springmvc
查看>>
我的小爬虫—cocoa 中的正则表达式
查看>>
HTML5 中 div 和section以及 article 的不同之处
查看>>
Yii2学习笔记之场景
查看>>
CS Website
查看>>
docker - 容器里安装ssh
查看>>
Ant design 组件开发
查看>>
完整性约束
查看>>
docker 17.09.0-ce 启动更换网络地址
查看>>
关于《大道至简》第六章的收获
查看>>
JavaWeb部分面试题
查看>>
mac osx 系统开发php 的一些工具
查看>>
Tcp的三次握手,以及原理详解
查看>>
sprintboot 中占位符及多环境配置
查看>>
Oracle资源
查看>>
你需要一点点CIL
查看>>
java连接mysql的一个小例子
查看>>
laravel queue 修改之后不生效的坑
查看>>