Analog test micro-communication interface and WeChat development test Code _php Tutorial

Source: Internet
Author: User
Tags arraycon cdata sprintf
To become a public number (subscription number or service number) developer, you need to first verify the interface, which can be set after login https://mp.weixin.qq.com background. But I was in trouble, so I developed an interface class that contained validation functions (as well as the ability to reply to textual and graphic information). In fact, the interface verification after becoming a developer is useless.
On the code, base class: Weixin.class.php
Class Weixin
{
Public $token = ";//token
Public $debug = false;//is the status indicator of debug, which allows us to record some intermediate data while debugging
Public $setFlag = false;
Public $msgtype = ' text '; (' text ', ' image ', ' location ')
Public $msg = Array ();
Public function __construct ($token, $debug)
{
$this->token = $token;
$this->debug = $debug;
}
Get messages from users (message content and message type)
Public Function getmsg ()
{
$POSTSTR = $GLOBALS ["Http_raw_post_data"];
if ($this->debug)
{
$this->write_log ($POSTSTR);
}
if (!empty ($POSTSTR))
{
$this->msg = (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 = "
{$this->msg[' Fromusername ']}
{$this->msg[' Tousername ']}
{$CreateTime}
text
%s
%s
";
Return sprintf ($TEXTTPL, $text, $FuncFlag);
}
Reply to a text message based on an array parameter
Public Function Makenews ($newsData =array ())
{
$CreateTime = time ();
$FuncFlag = $this->setflag? 1:0;
$newTplHeader = "
{$this->msg[' Fromusername ']}
{$this->msg[' Tousername ']}
{$CreateTime}
news
%s
%s ";
$newTplItem = "
<! [cdata[%s]]>
%s
%s
%s
";
$newTplFoot = "
%s
";
$Content = ";
$itemsCount = count ($newsData);
$itemsCount = $itemsCount < 10? $itemsCount: 10;//public Platform message reply up to 10 records at a time
if ($itemsCount)
{
foreach ($newsData 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 (' certification failed ');
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 the debugging information, please refine it for intermediate debugging
}
}
?>

Code for the interface: weixin.php
Header ("Content-type:text/html;charset=utf-8");
Include_once (' weixin.class.php '); Referencing a newly defined message processing class
Define ("TOKEN", "Itwatch"); Mmhelper
Define (' DEBUG ', false);
$weixin = new Weixin (TOKEN, DEBUG); Instantiation of
$weixin->valid ();
$weixin->getmsg ();
$type = $weixin->msgtype; Message type
$username = $weixin->msg[' fromusername '); Which user sent you the message, this $username is encrypted, but each user is one by one corresponding
if ($type = = = ' text ')
{
if ($weixin->msg[' Content ']== ' Hello2bizuser ')
if ($weixin->msg[' Content ']== ' hello ')
{//When the user first pays attention to your account, your public account will be subject to a message that reads ' Hello2bizuser '.
$reply = $weixin->maketext (' Welcome to the public platform of Mesh Horizon Prestige ');
}
Else
{//This is where the user entered the text message
$keyword = $weixin->msg[' Content ']; User's text message content
Include_once ("chaxun.php"); A text message invokes a query program
$chaxun = new Chaxun (DEBUG, $keyword, $username);
$results [' items '] = $chaxun->search (); Code of the query
$reply = $weixin->makenews ($results);
$arrayCon = Array (
Array
"Title" = "Computer Learning Network",
"Description" = "100,000 Why-computer Learning Network",
"Picurl" = "http://www.veryphp.cn/datas/userfiles/8bd108c8a01a892d129c52484ef97a0d/images/website13.jpg",
"Url" = "http://www.why100000.com/"
),
Array
"Title" = "Very PHP learning net",
"Description" + "large PHP learning sharing community",
"Picurl" = "http://www.veryphp.cn/datas/userfiles/8bd108c8a01a892d129c52484ef97a0d/images/php01.jpg",
"Url" = "http://www.veryphp.cn/"
)
);
$results = $arrayCon;
$reply = $weixin->makenews ($results);
}
}
ElseIf ($type = = = ' Location ')
{
The user sends the location information to be processed later
}
ElseIf ($type = = = ' Image ')
{
The user sends the picture to be processed later
}elseif ($type = = = ' Voice ')
{
The user sends the sound to be processed later
}
//
$weixin->reply ($reply);
?>

Verify the code of the interface, complete with the curl function, and need to open PHP's curl extension. Put the//$weixin->valid in the weixin.php file (); The comment of one sentence can be verified, and the sentence can be commented out.






Header ("Content-type:text/html;charset=utf-8");
Preparing data
Define (' token ', ' itwatch ');//The token that you define is the private key of a communication
$echostr = ' Return this data indicates correct. ';
$timestamp = (string) time (); itself is an integer and must be converted to a string
$nonce = ' my-nonce ';
$signature = Signature (TOKEN, $timestamp, $nonce);


Function signature ($token, $timestamp, $nonce)
{
$tmpArr = array ($token, $timestamp, $nonce);
Sort ($t Mparr);
$tmpStr = implode ($TMPARR);
$tmpStr = SHA1 ($TMPSTR);
return $tmpStr;
}
//Submit
$post _data = Array (
"signature= $signature",
"timestamp= $timestamp",
"nonce= $nonce",
"echostr= $echostr"
);
$post _data = Implode (' & ', $post _data);
$url = ' http://www.veryphp.cn/tools/weixin/weixin.php ';
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url. '? '. $post _data); Simulates the Get Method
Ob_start ();
Curl_exec ($ch);
$result = Ob_get_contents ();
Ob_end_clean ();
Echo $result;
?

The core code above is weixin.class.php and weixin.php two files, I debug successfully, has been deployed on my server. If you want to test, use your phone to listen to the number: Itwatch, and then enter "Hello", will return the string: You are welcome to focus on the mesh horizon Prestige public platform. Just enter it and a text message will open.

OK, I admit that the above code is very messy, because I am very sleepy, to sleep. But the above code is really working, is a typical implementation of the principle of testing code. Hope to provide developers with a way of thinking, after reading can be combined with a database to write a fully functional information management program.
There is a service number, you can develop a menu on this basis, and then invoke the same code developed in response to the message reply system. It's actually very simple.
This is the Real network communication program, than you write the enterprise station, the data into, and then in order to retrieve the paging display, to be more interesting.

Mesh-Zhang Qing
2013-12-3?

http://www.bkjia.com/PHPjc/621644.html www.bkjia.com true http://www.bkjia.com/PHPjc/621644.html techarticle to become a public number (subscription number or service number) developer, you need to first verify the interface, which can be set after login https://mp.weixin.qq.com background. But I'm in trouble .

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.