Home >Backend Development >PHP Tutorial > 微信大众平台接口

微信大众平台接口

WBOY
WBOYOriginal
2016-06-13 12:44:35971browse

微信公众平台接口

<?php <br />
/**<br>
  * wechat php test<br>
  */<br>
<br>
//define your token<br>
define("TOKEN", "qianshou");<br>
$wechatObj = new wechatCallbackapiTest();<br>
$wechatObj->valid();<br>
$wechatObj->responseMsg();<br>
<br>
class wechatCallbackapiTest<br>
{<br>
	public function valid()<br>
    {<br>
        $echoStr = $_GET["echostr"];<br>
<br>
        //valid signature , option<br>
        if($this->checkSignature()){<br>
        	echo $echoStr;<br>
        	exit;<br>
        }<br>
    }<br>
<br>
    public function responseMsg()<br>
    {<br>
		//get post data, May be due to the different environments<br>
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];<br>
<br>
      	//extract post data<br>
		if (!empty($postStr)){<br>
                <br>
              	$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);<br>
                $fromUsername = $postObj->FromUserName;<br>
                $toUsername = $postObj->ToUserName;<br>
                $keyword = trim($postObj->Content);<br>
                $time = time();<br>
                $textTpl = "<xml><br>
							<tousername></tousername><br>
							<fromusername></fromusername><br>
							<createtime>%s</createtime><br>
							<msgtype></msgtype><br>
							<content></content><br>
							<funcflag>0</funcflag><br>
							</xml>";             <br>
				if(!empty( $keyword ))<br>
                {<br>
              		$msgType = "text";<br>
                	$contentStr = "Welcome to wechat world!";<br>
                	$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);<br>
                	echo $resultStr;<br>
                }else{<br>
                	echo "Input something...";<br>
                }<br>
<br>
        }else {<br>
        	echo "qianshouerror"; <div class="clear">
                 
              
              
        
            </div>
Statement:
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