我用的如下代码。。。不知道哪里错了 PHP小白 token验证是通过的 目的是关注公众号后自动发一个图文 求助啊 正确的应该怎么写
<?php
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
$tmpstr = $postArr;
//处理消息,并设置回复类型
$postObj = simplexml_load_string($postArr);
if(strtolower($postObj->MsgType)=='event'){
if (strtolower($postObj->MsgType) == "text" && trim($postObj->Content == "单图文")){
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$array = array(
array(
"title"=>"XXX",
"description"=>"XXXXXXX",
"picUrl"=>"httpXXXXn1.jpg",
"url"=>"httpsXXX.com/",
),
);
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>".count($array)."</ArticleCount>
<Articles>";
foreach ($array as $key => $v) {
$template .= "<item>
<Title><![CDATA[".$v['title']."]]></Title>
<Description><![CDATA[".$v['description']."]]></Description>
<PicUrl><![CDATA[".$v['picUrl']."]]></PicUrl>
<Url><![CDATA[".$v['url']."]]></Url>
</item>";
}
$template.= "</Articles></xml>";
$time = time();
echo sprintf( $template,$toUser,$fromUser,$time,"news");
}else {
echo "";
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;
}
}
}
?>
https://www.cnblogs.com/xqschool/p/6745113.html
<?php
header("Content-type: text/html; charset=utf-8");
$nonce = $_GET['nonce'];
$token = 'winxin';
$timestamp = $_GET['timestamp'];
if (isset($_GET['echostr'])) {
if (bindServerCheck()) {
echo $_GET['echostr'];
}
exit();
}
responseMsg();
//消息回复
function responseMsg() {
//1.获取到微信推送过来post数据(xml格式)
$postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
//2.处理消息类型,并设置回复类型和内容
$postObj = simplexml_load_string($postArr, 'SimpleXMLElement', LIBXML_NOCDATA);
//判断该数据包是否是订阅de事件推送
if (strtolower($postObj->MsgType) == 'event') {
//如果是关注 subscribe事件
if (strtolower($postObj->Event) == 'subscribe') {
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time = time();
$msgType = 'text';
$content = '欢迎关注我的微信公众号 test 123';
//修改为
if (is_utf8($content)) {
$content = $content;
} else {
$content = iconv('gb2312', 'UTF-8//IGNORE', $content);
}
$template = "
<![CDATA[%s]]>
<![CDATA[%s]]>
%s
<![CDATA[%s]]>
<![CDATA[%s]]>
";
$info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
echo $info;
}
}
}
// 开发者模式绑定校验
function bindServerCheck($token) {
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$tmpArr = array(
$token,
$timestamp,
$nonce
);
sort($tmpArr);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if ($tmpStr == $signature) {
return true;
} else {
return false;
}
}
function to_utf8($in)
{
if (is_array($in)) {
foreach ($in as $key => $value) {
$out[to_utf8($key)] = to_utf8($value);
}
} elseif(is_string($in)) {
if(mb_detect_encoding($in) != "UTF-8")
return utf8_encode($in);
else
return $in;
} else {
return $in;
}
return $out;
}
function is_utf8($str)
{
return preg_match('//u', $str);
}
?>