微信官方PHP接口无法获得postStr数据

在微信官方提供的PHP接口文档中,处理主要业务有这么一段代码

public function getIndex(){
   
        if (ob_get_contents())  ob_end_clean();
        $postStr = isset($GLOBALS["HTTP_RAW_POST_DATA"])?$GLOBALS["HTTP_RAW_POST_DATA"]:'';//获取post过来的数据

        if (!empty($postStr)){
            $postObj = simplexm
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信开放平台提供了多种开发接口和SDK,可以根据具体的需求选择使用。以下是一个简单的示例代码,用于实现微信公众号的后台服务器: ``` <?php // 验证接口配置信息 $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = "YOUR_TOKEN"; // 在接口配置信息中设置的Token $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode($tmpArr); $tmpStr = sha1($tmpStr); if ($tmpStr == $signature) { echo $_GET["echostr"]; } else { echo "Error!"; } // 处理用户发送的消息 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)) { $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $msgType = $postObj->MsgType; $content = trim($postObj->Content); $time = time(); if ($msgType == "text") { // 处理文本消息 $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $response = sprintf($textTpl, $fromUsername, $toUsername, $time, "你发送的消息是:" . $content); echo $response; } else { // 处理其他类型的消息 // ... } } ?> ``` 以上代码实现了微信公众号的接口验证和处理用户发送的文本消息。需要注意的是,上述代码仅供参考,具体的实现方式和业务逻辑需要根据具体需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值