一、寫好接口程序
在你的服務(wù)器上上傳好一個(gè)接口程序文件,如http://www.yourdomain.com/weixin.php 內(nèi)容如下:
復(fù)制代碼代碼如下:
define("TOKEN", "weixin");//自己定義的token 就是個(gè)通信的私鑰
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
//$wechatObj->responseMsg();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
$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();
$textTpl = "
%s
0
";
if(!empty( $keyword ))
{
$msgType = "text";
$contentStr = '你好啊,屌絲';
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo '咋不說哈呢';
}
}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;
}
}
}
?>
二、配置微信公眾平臺回復(fù)接口
設(shè)置回復(fù)接口,填好URL和Token(url填上面的http://www.yourdomain.com/weixin.php,token必須跟上面程序里面定義的Token一致)
三、驗(yàn)證接口
用自己的個(gè)人微信關(guān)注下你的公眾賬號,給這個(gè)賬號發(fā)一條消息過去,收到原樣的消息返回,即驗(yàn)證成功了。
四、開始自定義回復(fù)
注釋掉$wechatObj->valid(); 這行,同時(shí)去掉//$wechatObj->responseMsg();這行的注釋。
你可以修改responseMsg函數(shù)里面的代碼,根據(jù)用戶的消息類型('text','image','location')和消息內(nèi)容來回復(fù)用戶不同的內(nèi)容。
消息接口就可以使用了,發(fā)個(gè)消息試試看吧?
1.封裝weixin.class.php
由于微信公眾平臺的通信使用的是特定格式的XML數(shù)據(jù),每次接受和回復(fù)都要去做一大堆的數(shù)據(jù)處理。
我們就考慮在這個(gè)基礎(chǔ)上做一次封裝,weixin.class.php,代碼如下:
復(fù)制代碼代碼如下:
class Weixin
{
public $token = '';//token
public $debug = false;//是否debug的狀態(tài)標(biāo)示,方便我們在調(diào)試的時(shí)候記錄一些中間數(shù)據(jù)
public $setFlag = false;
public $msgtype = 'text'; //('text','image','location')
public $msg = array();
public function __construct($token,$debug)
{
$this->token = $token;
$this->debug = $debug;
}
//獲得用戶發(fā)過來的消息(消息內(nèi)容和消息類型 )
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']);
}
}
//回復(fù)文本消息
public function makeText($text='')
{
$CreateTime = time();
$FuncFlag = $this->setFlag ? 1 : 0;
$textTpl = "
msg['FromUserName']}]]>
msg['ToUserName']}]]>
{$CreateTime}
1
]>
%s
";
return sprintf($textTpl,$text,$FuncFlag);
}
//根據(jù)數(shù)組參數(shù)回復(fù)圖文消息
public function makeNews($newsdata=array())
{
$CreateTime = time();
$FuncFlag = $this->setFlag ? 1 : 0;
$newTplHeader = "
msg['FromUserName']}]]>
msg['ToUserName']}]]>
{$CreateTime}
%s";
$newTplItem = "
";
$newTplFoot = "
%s
";
$Content = '';
$itemsCount = count($newsdata['items']);
$itemsCount = $itemsCount < 10 ? $itemsCount : 10;//微信公眾平臺圖文回復(fù)的消息一次最多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('認(rèn)證失敗');
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){
//這里是你記錄調(diào)試信息的地方 請自行完善 以便中間調(diào)試
}
}
?>
2.調(diào)用weixin.class.php
把你的微信公眾平臺主接口文件(如前面定義的http://www.yourdomain.com/weixin.php)中,修改代碼為:
復(fù)制代碼代碼如下:
include_once('weixin.class.php');//引用剛定義的微信消息處理類
define("TOKEN", "mmhelper");
define('DEBUG', true);
$weixin = new Weixin(TOKEN,DEBUG);//實(shí)例化
$weixin->getMsg();
0 = $weixin->msgtype;//消息類型
$username = $weixin->msg['FromUserName'];//哪個(gè)用戶給你發(fā)的消息,這個(gè)$username是微信加密之后的,但是每個(gè)用戶都是一一對應(yīng)的
if (0==='text') {
if ($weixin->msg['Content']=='Hello2BizUser') {//微信用戶第一次關(guān)注你的賬號的時(shí)候,你的公眾賬號就會受到一條內(nèi)容為'Hello2BizUser'的消息
$reply = $weixin->makeText('歡迎你關(guān)注哦,屌絲');
}else{//這里就是用戶輸入了文本信息
$keyword = $weixin->msg['Content']; //用戶的文本消息內(nèi)容
include_once("chaxun.php");//文本消息 調(diào)用查詢程序
$chaxun= new chaxun(DEBUG,$keyword,$username);
$results['items'] =$chaxun->search();//查詢的代碼
$reply = $weixin->makeNews($results);
}
}elseif (0==='location') {
//用戶發(fā)送的是位置信息 稍后的文章中會處理
}elseif (0==='image') {
//用戶發(fā)送的是圖片 稍后的文章中會處理
}elseif (0==='voice') {
//用戶發(fā)送的是聲音 稍后的文章中會處理
}
$weixin->reply($reply);
?>
3.查詢代碼
還需要將數(shù)據(jù)庫里面的查詢結(jié)果格式化為特定的形式
復(fù)制代碼代碼如下:
public function search(){
$record=array(); //定義返回結(jié)果的數(shù)組
$list = $this->search($this->keyword);//普通的根據(jù)關(guān)鍵詞查詢數(shù)據(jù)庫的操作 代碼就不用分享了
if(is_array($list)&&!empty($list)){
foreach($list as $msg){
$record[]=array(//以下代碼,將數(shù)據(jù)庫中查詢返回的數(shù)組格式化為微信返回消息能接收的數(shù)組形式,即title、description、picurl、url 詳見微信官方的文檔描述
'title' =>$msg['title'],
'description' =>$msg['discription'],
'picurl' => $msg['pic_url'],
'url' =>$msg['url']
);
}
}
return $record;
}
?>
在你的服務(wù)器上上傳好一個(gè)接口程序文件,如http://www.yourdomain.com/weixin.php 內(nèi)容如下:
復(fù)制代碼代碼如下:
define("TOKEN", "weixin");//自己定義的token 就是個(gè)通信的私鑰
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
//$wechatObj->responseMsg();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
$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();
$textTpl = "
%s
0
";
if(!empty( $keyword ))
{
$msgType = "text";
$contentStr = '你好啊,屌絲';
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo '咋不說哈呢';
}
}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;
}
}
}
?>
二、配置微信公眾平臺回復(fù)接口
設(shè)置回復(fù)接口,填好URL和Token(url填上面的http://www.yourdomain.com/weixin.php,token必須跟上面程序里面定義的Token一致)
三、驗(yàn)證接口
用自己的個(gè)人微信關(guān)注下你的公眾賬號,給這個(gè)賬號發(fā)一條消息過去,收到原樣的消息返回,即驗(yàn)證成功了。
四、開始自定義回復(fù)
注釋掉$wechatObj->valid(); 這行,同時(shí)去掉//$wechatObj->responseMsg();這行的注釋。
你可以修改responseMsg函數(shù)里面的代碼,根據(jù)用戶的消息類型('text','image','location')和消息內(nèi)容來回復(fù)用戶不同的內(nèi)容。
消息接口就可以使用了,發(fā)個(gè)消息試試看吧?
1.封裝weixin.class.php
由于微信公眾平臺的通信使用的是特定格式的XML數(shù)據(jù),每次接受和回復(fù)都要去做一大堆的數(shù)據(jù)處理。
我們就考慮在這個(gè)基礎(chǔ)上做一次封裝,weixin.class.php,代碼如下:
復(fù)制代碼代碼如下:
class Weixin
{
public $token = '';//token
public $debug = false;//是否debug的狀態(tài)標(biāo)示,方便我們在調(diào)試的時(shí)候記錄一些中間數(shù)據(jù)
public $setFlag = false;
public $msgtype = 'text'; //('text','image','location')
public $msg = array();
public function __construct($token,$debug)
{
$this->token = $token;
$this->debug = $debug;
}
//獲得用戶發(fā)過來的消息(消息內(nèi)容和消息類型 )
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']);
}
}
//回復(fù)文本消息
public function makeText($text='')
{
$CreateTime = time();
$FuncFlag = $this->setFlag ? 1 : 0;
$textTpl = "
msg['FromUserName']}]]>
msg['ToUserName']}]]>
{$CreateTime}
1
]>
%s
";
return sprintf($textTpl,$text,$FuncFlag);
}
//根據(jù)數(shù)組參數(shù)回復(fù)圖文消息
public function makeNews($newsdata=array())
{
$CreateTime = time();
$FuncFlag = $this->setFlag ? 1 : 0;
$newTplHeader = "
msg['FromUserName']}]]>
msg['ToUserName']}]]>
{$CreateTime}
%s";
$newTplItem = "
";
$newTplFoot = "
%s
";
$Content = '';
$itemsCount = count($newsdata['items']);
$itemsCount = $itemsCount < 10 ? $itemsCount : 10;//微信公眾平臺圖文回復(fù)的消息一次最多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('認(rèn)證失敗');
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){
//這里是你記錄調(diào)試信息的地方 請自行完善 以便中間調(diào)試
}
}
?>
2.調(diào)用weixin.class.php
把你的微信公眾平臺主接口文件(如前面定義的http://www.yourdomain.com/weixin.php)中,修改代碼為:
復(fù)制代碼代碼如下:
include_once('weixin.class.php');//引用剛定義的微信消息處理類
define("TOKEN", "mmhelper");
define('DEBUG', true);
$weixin = new Weixin(TOKEN,DEBUG);//實(shí)例化
$weixin->getMsg();
0 = $weixin->msgtype;//消息類型
$username = $weixin->msg['FromUserName'];//哪個(gè)用戶給你發(fā)的消息,這個(gè)$username是微信加密之后的,但是每個(gè)用戶都是一一對應(yīng)的
if (0==='text') {
if ($weixin->msg['Content']=='Hello2BizUser') {//微信用戶第一次關(guān)注你的賬號的時(shí)候,你的公眾賬號就會受到一條內(nèi)容為'Hello2BizUser'的消息
$reply = $weixin->makeText('歡迎你關(guān)注哦,屌絲');
}else{//這里就是用戶輸入了文本信息
$keyword = $weixin->msg['Content']; //用戶的文本消息內(nèi)容
include_once("chaxun.php");//文本消息 調(diào)用查詢程序
$chaxun= new chaxun(DEBUG,$keyword,$username);
$results['items'] =$chaxun->search();//查詢的代碼
$reply = $weixin->makeNews($results);
}
}elseif (0==='location') {
//用戶發(fā)送的是位置信息 稍后的文章中會處理
}elseif (0==='image') {
//用戶發(fā)送的是圖片 稍后的文章中會處理
}elseif (0==='voice') {
//用戶發(fā)送的是聲音 稍后的文章中會處理
}
$weixin->reply($reply);
?>
3.查詢代碼
還需要將數(shù)據(jù)庫里面的查詢結(jié)果格式化為特定的形式
復(fù)制代碼代碼如下:
public function search(){
$record=array(); //定義返回結(jié)果的數(shù)組
$list = $this->search($this->keyword);//普通的根據(jù)關(guān)鍵詞查詢數(shù)據(jù)庫的操作 代碼就不用分享了
if(is_array($list)&&!empty($list)){
foreach($list as $msg){
$record[]=array(//以下代碼,將數(shù)據(jù)庫中查詢返回的數(shù)組格式化為微信返回消息能接收的數(shù)組形式,即title、description、picurl、url 詳見微信官方的文檔描述
'title' =>$msg['title'],
'description' =>$msg['discription'],
'picurl' => $msg['pic_url'],
'url' =>$msg['url']
);
}
}
return $record;
}
?>
樂發(fā)網(wǎng)超市批發(fā)網(wǎng)提供超市貨源信息,超市采購進(jìn)貨渠道。超市進(jìn)貨網(wǎng)提供成都食品批發(fā),日用百貨批發(fā)信息、微信淘寶網(wǎng)店超市采購信息和超市加盟信息.打造國內(nèi)超市采購商與批發(fā)市場供應(yīng)廠商搭建網(wǎng)上批發(fā)市場平臺,是全國批發(fā)市場行業(yè)中電子商務(wù)權(quán)威性網(wǎng)站。
本文內(nèi)容整合網(wǎng)站:百度百科、知乎、淘寶平臺規(guī)則