WeChat follow auto reply follow message php

<?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();


 //Message reply
function responseMsg() {
    //1. Get the post data (xml format)
    pushed by WeChat $postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
    //2. Process the message type and set the reply type and content
    $postObj = simplexml_load_string($postArr, 'SimpleXMLElement ', LIBXML_NOCDATA);
    //Determine whether the packet is subscribed to event push
    if (strtolower($postObj-> MsgType) == 'event') {
        //If you are concerned about the subscribe event
        if (strtolower($postObj->Event) == 'subscribe') {
            $toUser = $postObj->FromUserName;
            $fromUser = $postObj->ToUserName;
            $time = time();
            $ msgType = 'text';
            $content = 'Welcome to follow my WeChat public account test 123';              //modify to               if (is_utf8($content)) {                 $content = $content;               } else {                $content = iconv('gb2312 ', 'UTF-8//IGNORE', $content);               }             $template = "<xml>                             <ToUserName><![CDATA[%s]]></ToUserName>



 









                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <Content><![CDATA[%s]]></Content>
                            </xml>";
            $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);
}




?>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325970288&siteId=291194637