1 有时候我们需要调试线上公众号,微信公众号请求我们的接口回调之后,输出的内容到底是什么?有没有报错信息?xml是否是对的?我们可以模拟微信公众号的回调请求。代码如下。
<?php
set_time_limit(0);
ini_set('memory_limit', '128000M');
$time = time();
$xml = '<xml><ToUserName><![CDATA[gh_5078da199412]]></ToUserName> <FromUserName><![CDATA[oz_WAjnyPnPFE1VnSSDgQby2V1fY]]></FromUserName> <CreateTime>'.$time.'</CreateTime> <MsgType><![CDATA[event]]></MsgType> <Event><![CDATA[SCAN]]></Event> <EventKey><![CDATA[WxQrcodeRelation_5725690163]]></EventKey> <Ticket><![CDATA[gQHT8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyZXI1X1lsZlJhMGUxMDAwKGcwS00AAgDny2BaAwQAAAAA]]></Ticket> </xml>';
$header[]="Content-Type: text/xml; charset=utf-8";
$header[]="User-Agent: Apache/1.3.26 (Unix)";
$header[]="Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2";
$header[]="Connection: keep-alive";
$header[]="Content-Length: ".strlen($xml);
$ch = curl_init();
$url = 'http://openpf.xxxx.com/wx/gate4space?signature=db5025bc3f75bcb4f16dacc8bb3f5480fc504c5d×tamp='.$time.'&nonce=623483653&openid=oz_WAjnyPnPFE1VnSSDgQby2V1fY';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$res = curl_exec($ch);
//$request_header = curl_getinfo( $ch, CURLINFO_HEADER_OUT);
//print_r($request_header);
//$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
// 根据头大小去获取头信息内容
//$header = substr($sContent, 0, $headerSize);
curl_close($ch);
header('Content-Type:text/xml; charset=utf-8');
echo ($res);
$url = 'http://openpf.xxxx.com/wx/gate4space?signature=db5025bc3f75bcb4f16dacc8bb3f5480fc504c5d×tamp='.$time.'&nonce=623483653&openid=oz_WAjnyPnPFE1VnSSDgQby2V1fY'; 是公众号后台配置的:服务器地址(URL)。
效果如下:
技术交流
CleverCode是一名架构师,技术交流,咨询问题,请加CleverCode创建的qq群(架构师俱乐部):517133582。加群和腾讯,阿里,百度,新浪等公司的架构师交流。【架构师俱乐部】宗旨:帮助你成长为架构师!