我是用thinkphp开发的,这是消息推送地址的代码:
<?php
namespace Home\Controller;
use Think\Controller;
class WeixinController extends Controller {
var $data = array ();
var $wxcpt, $sReqTimeStamp, $sReqNonce, $sEncryptMsg;
public function index() {
// 删除微信传递的token干扰
unset ( $_REQUEST ['token'] );
$appid = '**********';
$token = "baofan1994";
$encodingaeskey = "*********************************";
$content = wp_file_get_contents ( 'php://input' );
! empty ( $content ) || die ( '这是微信请求的接口地址,直接在浏览器里无效' );
if ($_GET ['encrypt_type'] == 'aes') {
vendor ( 'WXBiz.wxBizMsgCrypt' );
$this->sReqTimeStamp = I ( 'get.timestamp' );
$this->sReqNonce = I ( 'get.nonce' );
$this->sEncryptMsg = I ( 'get.msg_signature' );
$this->wxcpt = new \WXBizMsgCrypt ( $token, $encodingaeskey, $appid);
$sMsg = ""; // 解析之后的明文
$errCode = $this->wxcpt->DecryptMsg ( $this->sEncryptMsg, $this->sReqTimeStamp, $this->sReqNonce, $content, $sMsg );
if ($errCode != 0) {
exit ();
} else {
// 解密成功,sMsg即为xml格式的明文
$content = $sMsg;
}
}
$data = new \SimpleXMLElement ( $content );
// $data || die ( '参数获取失败' );
foreach ( $data as $key => $value ) {
$this->data [$key] = safe ( strval ( $value ) );
}
$this->replyText("好好学习");
}
/* 回复文本消息 */
public function replyText($content) {
$msg ['Content'] = $content;
$this->_replyData ( $msg, 'text' );
}
/* 发送回复消息到微信平台 */
private function _replyData($msg, $msgType) {
$msg ['ToUserName'] = $this->data ['FromUserName'];
$msg ['FromUserName'] = $this->data ['ToUserName'];
$msg ['CreateTime'] = NOW_TIME;
$msg ['MsgType'] = $msgType;
if ($_REQUEST ['doNotInit']) {
dump ( $msg );
exit ();
}
$xml = new \SimpleXMLElement ( '<xml></xml>' );
$this->_data2xml ( $xml, $msg );
$str = $xml->asXML ();
if ($_GET ['encrypt_type'] == 'aes') {
$sEncryptMsg = ""; // xml格式的密文
$errCode = $this->wxcpt->EncryptMsg ( $str, $this->sReqTimeStamp, $this->sReqNonce, $sEncryptMsg );
if ($errCode == 0) {
$str = $sEncryptMsg;
} else {
}
}
echo ($str);
}
/* 组装xml数据 */
public function _data2xml($xml, $data, $item = 'item') {
foreach ( $data as $key => $value ) {
is_numeric ( $key ) && ($key = $item);
if (is_array ( $value ) || is_object ( $value )) {
$child = $xml->addChild ( $key );
$this->_data2xml ( $child, $value, $item );
} else {
if (is_numeric ( $value )) {
$child = $xml->addChild ( $key, $value );
} else {
$child = $xml->addChild ( $key );
$node = dom_import_simplexml ( $child );
$node->appendChild ( $node->ownerDocument->createCDATASection ( $value ) );
}
}
}
}
}
其实我是想知道怎么去调试,代码肯定是有问题的。。。
可以通过写文件或者数据库的方式调试,我一般是写文件!比如代码的入口开始写文件,记录用户openid以及发送内容,然后以此类推,最极端的情况是每行代码后面都跟上调试信息,当然,这没必要哈!只需要在你感兴趣的有疑问的地方加就好了!如果前一个调试信息有了,后一个调试信息没出来,肯定是中间的代码有问题!另外记得检查下代码有没有语法错误之类的,在编辑器里打开看看