微信接口测试号问题解决方案

1、第一个问题,自己的服务器应该上传哪些代码,我在网上找了一个能用的,如下:

(1)index.php

<?php  

date_default_timezone_set("Asia/Shanghai");  

define("TOKEN", "weixin");  
  
require_once("Utils.php");  

Utils::traceHttp();  
  
$wechatObj = new wechatCallBackapiTest();  

if (isset($_GET["echostr"])){  
    $wechatObj->valid();  
}else {  
    $wechatObj->responseMsg();  
}  
  
class wechatCallBackapiTest  
{  
    
    public function valid()  
    {  
        $echoStr = $_GET["echostr"];  
        if ($this->checkSignature()){  
            echo $echoStr;  
            exit;  
        }  
    }  
  
    
    private function checkSignature()  
    {  
        $signature = $_GET["signature"];  
        $timestamp = $_GET["timestamp"];  
        $nonce = $_GET["nonce"];  
  
        $token = TOKEN;  
        $tmpArr = array($token, $timestamp, $nonce);  
        sort($tmpArr);  
        $tmpStr = implode($tmpArr);  
        $tmpStr = sha1($tmpStr);  
  
        if ($tmpStr == $signature){  
            return true;  
        }else {  
            return false;  
        }  
    }  
  
    
    public function responseMsg()  
    {  
       
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];  
       
        Utils::logger($postStr);  
        if (!empty($postStr)){  
          
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);  
            $RX_TYPE = trim($postObj->MsgType);  
        
            switch($RX_TYPE){  
                case "event":  
                    $result = $this->receiveEvent($postObj);  
                    break;  
                default:  
                    $result = "unknow msg type:".$RX_TYPE;  
                    break;  
            }  
            
            Utils::logger($result, '公众号');  
            echo $result;  
        }else{  
            echo "";  
            exit;  
        }  
    }  
  
   
    private function receiveEvent($object)  
    {  
        switch ($object->Event){  
        
            case "subscribe":  
                $content = "欢迎关注微微一笑很倾城";  
                break;  
            default:  
                $content = "";  
                break;  
        }  
        $result = $this->transmitText($object, $content);  
        return $result;  
    }  
  
    
    private function transmitText($object, $content)  
    {  
        $xmlTpl = "<xml>  
    <ToUserName><![CDATA[%s]]></ToUserName>  
    <FromUserName><![CDATA[%s]]></FromUserName>  
    <CreateTime><![CDATA[%s]]></CreateTime>  
    <MsgType><![CDATA[text]]></MsgType>  
    <Content><![CDATA[%s]]></Content>  
</xml>";  
        $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content);  
        return $result;  
    }  
}  

(2)Utils.php

<?php  
class Utils  
{  
    public static function traceHttp()  
    {  
        $content = date('Y-m-d H:i:s')."\n\rremote_ip:".$_SERVER["REMOTE_ADDR"].  
            "\n\r".$_SERVER["QUERY_STRING"]."\n\r\n\r";  
        $max_size = 1000;  
        $log_filename = "./query.xml";  
        if (file_exists($log_filename) and (abs(filesize($log_filename))) > $max_size){  
            unlink($log_filename);  
        }else {  
  
        }  
        file_put_contents($log_filename, $content, FILE_APPEND);  
    }  
  
    public static function logger($log_content, $type = '用户')  
    {  
        $max_size = 3000;  
        $log_filename = "./log.xml";  
        if (file_exists($log_filename) and (abs(filesize($log_filename)) >  
                $max_size)) {  
            unlink($log_filename);  
        }  
        file_put_contents($log_filename, "$type  ".date('Y-m-d H:i:s')."\n\r".$log_content."\n\r",  
            FILE_APPEND);  
    }  
}  

(3)output_query.php

<?php  
@header('Content-type: text/plain;charset=UTF-8');  
$filepath = './query.xml';  
readfile($filepath);  

(4)output_log.php

<?php  
@header('Content-type: text/plain;charset=UTF-8');  
$filepath = './log.xml';  
readfile($filepath);  

2、代码上传了,但是还是配置失败

原因:(1)服务器没有安装apache,这个自己去网上百度,一大堆;(2)没有安装php运行环境,附个链接:点击打开链接

(3)路径要填写对,前面要加http://或者https://

改完这些测试,很好用的一个debug:点击打开链接

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值