要成為微信公眾號(訂閱號或服務號)的開發者,需要首先驗證接口,這個可以在登錄微信https://mp.weixin.qq.com后臺后設置。但是我嫌麻煩,于是開發個接口類,包含驗證函數(還有回復文本信息和圖文信息的功能)。其實接口驗證在成為開發者之后就沒用了。
加密 小程序接口 php?上代碼,微信基類:weixin.class.php
<>
class weixin
{
public $token = '';//token
public $debug = false;//是否debug的狀態標示,方便我們在調試的時候記錄一些中間數據
public $setflag = false;
public $msgtype = 'text'; //('text','image','location')
public $msg = array();
public function __construct($token,$debug)
{
$this->token = $token;
$this->debug = $debug;
}
//獲得用戶發過來的消息(消息內容和消息類型 )
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']);
}
}
//回復文本消息
public function maketext($text='')
{
$createtime = time();
$funcflag = $this->setflag ? 1 : 0;
$texttpl = "msg['fromusername']}]]>
msg['tousername']}]]>
{$createtime}
%s";
return sprintf($texttpl,$text,$funcflag);
}
//根據數組參數回復圖文消息
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);
$itemscount = $itemscount < 10="" $itemscount="" :="">
if ($itemscount)
{
foreach ($newsdata as $key => $item)
{
if ($key<>
{
$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)
{
//這里是你記錄調試信息的地方 請自行完善 以便中間調試
}
}
?>
微信接口的代碼:weixin.php
<>
header("content-type: text/html;charset=utf-8");
include_once('weixin.class.php'); //引用剛定義的微信消息處理類
define("token", "itwatch"); //mmhelper
define('debug', false);
$weixin = new weixin(token, debug); //實例化
//$weixin->valid();
$weixin->getmsg();
$type = $weixin->msgtype; //消息類型
$username = $weixin->msg['fromusername']; //哪個用戶給你發的消息,這個$username是微信之后的,但是每個用戶都是一一對應的
if ($type==='text')
{
//if ($weixin->msg['content']=='hello2bizuser')
if ($weixin->msg['content']=='你好')
{ //微信用戶第一次關注你的賬號的時候,你的公眾賬號就會受到一條內容為'hello2bizuser'的消息
$reply = $weixin->maketext('歡迎你關注網眼視界威信公眾平臺');
}
else
{ //這里就是用戶輸入了文本信息
$keyword = $weixin->msg['content']; //用戶的文本消息內容
//include_once("chaxun.php"); //文本消息 調用查詢程序
//$chaxun= new chaxun(debug, $keyword, $username);
//$results['items'] =$chaxun->search(); //查詢的代碼
//$reply = $weixin->makenews($results);
$arraycon = array(
array(
"title"=>"電腦學習網",
"description"=>"十萬個為什么-電腦學習網",
"picurl"=>"https://www.veryphp.cn/datas/userfiles/8bd108c8a01a892d129c52484ef97a0d/images/website13.jpg",
"url"=>"https://www.why100000.com/"
),
array(
"title"=>"非常php學習網",
"description"=>"大型php學習分享社區",
"picurl"=>"https://www.veryphp.cn/datas/userfiles/8bd108c8a01a892d129c52484ef97a0d/images/php01.jpg",
"url"=>"https://www.veryphp.cn/"
)
);
$results = $arraycon;
$reply = $weixin->makenews($results);
}
}
elseif ($type==='location')
{
//用戶發送的是位置信息 稍后處理
}
elseif ($type==='image')
{
//用戶發送的是圖片 稍后處理
}elseif ($type==='voice')
{
//用戶發送的是聲音 稍后處理
}
//
$weixin->reply($reply);
?>
驗證微信接口的代碼,用 curl 函數完成,需要打開php的 curl 擴展。把 weixin.php 文件中的 //$weixin->valid(); 一句的注釋去掉即可驗證,完了把這句注釋掉即可。
<>
//header("content-type: text/html;charset=utf-8");
//準備數據
define('token', 'itwatch');//自己定義的token 就是個通信的私鑰
$echostr = '返回此數據表明正確。';
$timestamp = (string)time(); //本身為整數,必須轉換為字符串
$nonce = 'my-nonce';
$signature = signature(token, $timestamp, $nonce);
function signature($token, $timestamp, $nonce)
{
$tmparr = array($token, $timestamp, $nonce);
sort($tmparr);
$tmpstr = implode($tmparr);
$tmpstr = sha1($tmpstr);
return $tmpstr;
}
//提交
$post_data = array(
"signature=$signature",
"timestamp=$timestamp",
"nonce=$nonce",
"echostr=$echostr"
);
$post_data = implode('&',$post_data);
$url='https://www.veryphp.cn/tools/weixin/weixin.php';
$ch = curl_init();
curl_setopt($ch, curlopt_url, $url.'?'.$post_data); //模擬get方法
ob_start();
curl_exec($ch);
$result = ob_get_contents();
ob_end_clean();
echo $result;
?>
以上的核心代碼是 weixin.class.php 和 weixin.php 兩個文件,是我調試成功的,已經部署在我的服務器上了。你要測試的話,用手機微信收聽微信號:itwatch,然后輸入“你好”,會返回字符串:歡迎你關注網眼視界威信公眾平臺。隨便輸入,會打開一個圖文消息。
好吧,我承認以上代碼寫的非常凌亂,因為我十分瞌睡了, 要睡覺了。但以上代碼確實是能工作的,是典型的原理實現性測試代碼。希望給微信開發者提供個思路,看明白之后可以結合寫一個功能完善的微信信息后臺管理程序。。
有微信服務號的,可以在此基礎上開發個菜單,然后調用仿照以上代碼開發的消息回復。其實很簡單。
這才是真正的網絡通信程序,比你寫企業站,把數據輸進去,再按順序檢索出來分頁顯示,要有意思的多。
網眼-張慶
2013-12-3 ?
如您對本文有疑問或者有任何想說的,請點擊進行留言回復,萬千網友為您解惑!
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态