Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 微信消息接口使用,在提供的demo中引用自己写的类出现问题

微信消息接口使用,在提供的demo中引用自己写的类出现问题

Jun 23, 2016 pm 02:40 PM

这是微信提供的demo:我在responseMsg函数中想调用自己写的验证的php类,添加了这条语句
require_once "./IValidMsgImpl.php";却始终不响应,我刚学php,有很多java的思想,请phper帮我看看,我想不会有问题吧,要不就是路径问题?我用的是国外的服务器绑定的。
/**
  * wechat php test
  */

//define your token

define("TOKEN", "sharenet");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
$wechatObj->responseMsg();

class wechatCallbackapiTest
{
public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
         echo $echoStr;
        }
    }

    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)){
                //使用php内置函数读取xml文件并将其转化为对象
               $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
//通过对象读取xml消息的内容
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time()+(7*60*60);
$time2 = date('Y-m-d G:i:s', $time);
//定义回复消息的xml数据格式,其中中%s被替换的内容将不会被xml解析器解析
                $textTpl = "


%s


0
";
//这里只是简单的对用户发来的信息做了非空检验 详细的校验规则在这里展开
if(!empty( $keyword ))
                {
require_once "./IValidMsgImpl.php";
$test = new IValidMsgImpl();
$contentStr = $test->ValidNum($keyword);
//数据的回复格式不能修改!将回复内容转化为xml格式并发送
                 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time2, $msgType, $contentStr);
//将回复的内容$resultStr发送出去,腾讯微信服务器接收此xml格式的数据包并转发给接收者
                 echo $resultStr;
                }else{
$msgType = "text";
                     $contentStr =  "不要给我发空信息!";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time2, $msgType, $contentStr);
//将回复的内容$resultStr发送出去,腾讯微信服务器接收此xml格式的数据包并转发给接收者
echo $resultStr;
                }

        }else {
echo "get a error!";
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;
}
}
}

?>

我自己写的php:
public class IValidMsgImpl{
/*
验证微信好友发来的消息是否为学号,以验证是否导入学生信息
学号格式为10位数字如:1020107201
*/
function ValidNum($keyword){
//回复内容的数据类型:text,
$msgType = "text";
//自动回复的内容在这里修改即可$contentStr,发送汉字时要指定编码utf-8
//$contentStr = "i am a robot...";
if(preg_match("/^R:\d{10}$/",$keyword)){
$array = explode(':', $keyword);
$keyword2 = $array[1];
$contentStr = "你使用了R:命令,我已收到你的指令";
}else{
$contentStr = "欢迎你,你可以试试我的新功能,输入以'R:'命令并加上10位数字如:R:1020107228.就会有不同内容!";
return $contentStr;
}
}
?>
我只是想把验证的内容放到一个类中!现在已经测试过,微信好友发空的字符串是有提示的,代码就是在调用我自己php时出错,可能是路径问题,这两个php文件我都是放在apache服务器htdocs文件夹下的。求指导


回复讨论(解决方案)

require_once "./IValidMsgImpl.php";
这句是包含一个php文件。
你检查路径看看有无问题。

先看路径,在ValidMsgImpl.php中写个测试方法,比如
function isRight(){
echo 'test===========================>ok';
}

然后,在引入文件中调用,能显示那句的话说明引用路径正确,再找其他原因。不显示,先调整路径

我测试过,没问题,放在微信接口中就没用

就是测试过成功,然后在微信接口中调用就没反应

有学过php的大神吼一下,我具体的问题想请教请教

换环境出问题这种事别奇怪。有时在windows上编写代码,由于你用的编辑器的问题,再上传至linux服务器上后会出现特殊字符。这种特殊字符会导致语法错误。

谢谢你,问题解决了,其实是我一直纠结的是什么呢?给你讲一讲,互相提高。呵呵
 我的文件目录结构:根目录htdocs/下有一个文件myTest.php,同时有一个文件夹sharenet
sharenet/php/DAO/下放的是我的一些接口,里面有个IValidMsg.php接口
sharenet/php/impl/下放的是我的一些接口实现类,里面有个IValidMsgImpl.php接口实现类implements IValidMsg,而在文件里我加入引用路径require_once "../DAO/IValidMsg.php";

而我在myTest.php文件中时这样引用的require_once "./sharenet/php/impl/IValidMsgImpl.php";先要调用基于接口的实现类具体的方法,一运行就报错了,说路径错误
   找了好多资料才发现php中的路径问题很烦人,require只是把路径包含的文件直接加到当前文件中的,不像java,java中是边加载边编译的,而php是解释型的,先加载后翻译执行,他直接先把require_once "../DAO/IValidMsg.php";包含的路径先加载到myTest.php中的,然后运行时才编译的,这是是在htdocs/目录下的了,所以他找不到sharenet/php/impl/下的文件。所以我直接在定义接口实现类的文件中直接引用require_once "./sharenet/php/DAO/IValidMsg.php";路径了,听网上说可以把一个路径固化,set_include_path()这个函数?能固化当然好,直接将./sharenet/php/DAO/和./sharenet/php/impl/两个路径先预定义好,以后直接用就行了,而不再考虑文件里套文件而引发的路径问题了,现在我是这么想的,对你有没有点用?我是新学php的,你知道的话就跟我交流交流,我很想找个学过php的人学习经验啊!呵呵

换环境出问题这种事别奇怪。有时在windows上编写代码,由于你用的编辑器的问题,再上传至linux服务器上后会出现特殊字符。这种特殊字符会导致语法错误。
谢谢你,问题解决了,其实是我一直纠结的是什么呢?给你讲一讲,互相提高。呵呵
 我的文件目录结构:根目录htdocs/下有一个文件myTest.php,同时有一个文件夹sharenet
sharenet/php/DAO/下放的是我的一些接口,里面有个IValidMsg.php接口
sharenet/php/impl/下放的是我的一些接口实现类,里面有个IValidMsgImpl.php接口实现类implements IValidMsg,而在文件里我加入引用路径require_once "../DAO/IValidMsg.php";

而我在myTest.php文件中时这样引用的require_once "./sharenet/php/impl/IValidMsgImpl.php";先要调用基于接口的实现类具体的方法,一运行就报错了,说路径错误
   找了好多资料才发现php中的路径问题很烦人,require只是把路径包含的文件直接加到当前文件中的,不像java,java中是边加载边编译的,而php是解释型的,先加载后翻译执行,他直接先把require_once "../DAO/IValidMsg.php";包含的路径先加载到myTest.php中的,然后运行时才编译的,这是是在htdocs/目录下的了,所以他找不到sharenet/php/impl/下的文件。所以我直接在定义接口实现类的文件中直接引用require_once "./sharenet/php/DAO/IValidMsg.php";路径了,听网上说可以把一个路径固化,set_include_path()这个函数?能固化当然好,直接将./sharenet/php/DAO/和./sharenet/php/impl/两个路径先预定义好,以后直接用就行了,而不再考虑文件里套文件而引发的路径问题了,现在我是这么想的,对你有没有点用?我是新学php的,你知道的话就跟我交流交流,我很想找个学过php的人学习经验啊!呵呵

把这个注释掉$wechatObj->valid();

lz你好,我最近才接触微信的,注册了一个公众账号。看了微信网站上写着:新用户订阅,将由之前推送一条“Hello2BizUser”文本,变化为推送一条“subscribe”的事件。但是还是不懂写,比如别人关注我后  我要发一条“欢迎光临” 该怎么写呢  能写个demo.php吗,万分感谢

我自己用php做微信第三方开发,thinkphp做的一个平台,微发网。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? Apr 01, 2025 pm 03:09 PM

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

See all articles