Upload an interface program file on your server, such as http://www.yourdomain.com/weixin.php with the following content:
define("TOKEN", "weixin");//The token you define is a private key for communication
$wechatObj = new wechatCallbackapiTest();
$wechatObj- >valid();
//$wechatObj->responseMsg();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET[ "echostr"];
if($this->checkSignature()){
🎜 > {
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time(); ; < Content>
$keyword ))
$msgType = "text"; fromUsername, $toUsername, $time, $ msgtype, $ Contentstr);
Echo $ ResultStr;
} Else {
Echo ' Haha';
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;
2. Configure the WeChat public platform reply interface
Set the reply interface, fill in the URL and Token (the URL is filled in http://www.yourdomain.com/weixin.php above, and the token must be consistent with the Token defined in the above program)
3. Verification interface
Use your personal WeChat to follow your public account, send a message to this account, and receive the original message back, which means the verification is successful.
4. Start custom reply
Comment out the $wechatObj->valid(); line, and also remove the comment from the //$wechatObj->responseMsg(); line.
You can modify the code in the responseMsg function to reply to the user with different content based on the user's message type ('text', 'image', 'location') and message content.
The message interface is ready for use. Let’s try sending a message?
1. Encapsulate weixin.class.php
Since the communication on the WeChat public platform uses XML data in a specific format, a lot of data processing is required for each acceptance and reply.
We will consider making an encapsulation on this basis, weixin.class.php, the code is as follows:
Copy the code The code is as follows:
class Weixin
{
public $token = '';//token
public $debug = false;//Whether the debug status is marked to facilitate our debugging Record some intermediate data when
public $setFlag = false;
public $msgtype = 'text'; //('text','image','location')
public $msg = array( );
public function __construct($token,$debug)
{
$this->token = $token;
$this->debug = $debug;
}
//Get the message (message content and message type) sent by the user
public function getMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if ($this->debug) {
= (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->msgtype = strtolower($this->msg['MsgType']); > //Reply text message
public function makeText($text='')
{
$CreateTime = time();
$FuncFlag = $this->setFlag ? 1 : 0;
$textTpl = "
;FromUserName>msg['ToUserName']}]]>
1 ;
Reply Graphic news
public function makeNews($newsData=array())
{
$CreateTime = time();
$FuncFlag = $this->setFlag ? 1 : 0;
$newTplHeader = "
> ;msg['ToUserName']}]]>
%s";
$newTplItem = "
";
$newTplFoot = "
%s
";
$Content = '';
$itemsCount = count($newsData['items']);
$itemsCount = $itemsCount < 10 ? $itemsCount : 10;//微信公众平台图文回复的消息一次最多10条
if ($itemsCount) {
foreach ($newsData['items'] as $key => $item) {
if ($key<=9) {
$Content .= sprintf($newTplItem,$item['title'],$item['description'],$item['picurl'],$item['url']);
}
}
}
$header = sprintf($newTplHeader,$newsData['content'],$itemsCount);
$footer = sprintf($newTplFoot,$FuncFlag);
return $header . $Content . $footer;
}
public function reply($data)
{
if ($this->debug) {
$this->write_log($data);
}
echo $data;
}
public function valid()
{
if ($this->checkSignature()) {
if( $_SERVER['REQUEST_METHOD']=='GET' )
{
echo $_GET['echostr'];
exit;
}
}else{
write_log('认证失败');
exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET[" nonce"];
$tmpArr = array($this->token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode($tmpArr);
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
private function write_log($log){
//This is where you record debugging information. Please improve it yourself for intermediate debugging
}
}
?>
2. Call weixin.class.php
In your WeChat public platform main interface file (such as http://www.yourdomain.com/weixin.php defined earlier), modify the code to:
Copy Code The code is as follows:
include_once('weixin.class.php');//Refer to the WeChat message processing class just defined
define(" TOKEN", "mmhelper");
define('DEBUG', true);
$weixin = new Weixin(TOKEN,DEBUG);//Instantiation
$weixin->getMsg();
$type = $weixin->msgtype;//Message type
$username = $weixin->msg['FromUserName'];//Which user sent you the message, this $username is WeChat After encryption, but each user has a one-to-one correspondence
if ($type===='text') {
if ($weixin->msg['Content']=='Hello2BizUser' ) {//When a WeChat user follows your account for the first time, your public account will receive a message with the content 'Hello2BizUser'
$reply = $weixin->makeText('Welcome to follow, Diaosi');
}else{//This is where the user entered the text message
$keyword = $weixin->msg['Content']; //The user’s text message content
include_once(" chaxun.php");//Text message calls the query program
;//Query code
$reply = $weixin->makeNews($results);
}
}elseif ($type==='location') {
/ /The user sent location information, which will be processed in a later article.
}elseif ($type==='image') {
}elseif ($type==='voice') {
//The user sent a voice, which will be processed in a later article
}
$weixin->reply($reply);
?>
3. Query code
You also need to format the query results in the database into a specific form
Copy the code
The code is as follows:public function search(){
$record=array(); //Define the array of returned results
$list = $this->search($this-> ;keyword);//Ordinary querying the database based on keywords, the code does not need to be shared
if(is_array($list)&&!empty($list)){ {
$record[]=array(//The following code formats the array returned by the query in the database into an array form that can be received by WeChat return messages, that is, title, description, picurl, url. For details, please see the official WeChat document description
'picurl' => $msg['pic_url'] ,
'url' =>$msg['url']
🎜>?>
http://www.bkjia.com/PHPjc/745823.html
www.bkjia.com
truehttp: //www.bkjia.com/PHPjc/745823.html
TechArticle1. Write the interface program and upload an interface program file on your server, such as http://www The content of .yourdomain.com/weixin.php is as follows: Copy the code as follows: ?php define("TO...