php⽹站怎么对接,PHP对接公众平台消息接⼝开发
流程教程
PHP(外⽂名:PHP: Hypertext
Preprocessor,中⽂名:“超⽂本预处理器”)是⼀种通⽤开源脚本语⾔。语法吸收了C语⾔、Java和Perl的特点,利于学习,使⽤⼴泛,主要适⽤于Web开发领域。公众平台也运⽤到了,今天⼩编就来讲⼀讲PHP对接公众平台消息接⼝开发流程教程,供⼤家参考⼀下。
⼀、写好接⼝程序
复制代码 代码如下:
define("TOKEN", "weixin");//⾃⼰定义的token 就是个通信的私钥
$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
";
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;
}
}
}
>
⼆、配置公众平台回复接⼝
三、验证接⼝
⽤⾃⼰的个⼈关注下你的公众账号,给这个账号发⼀条消息过去,收到原样的消息返回,即验证成功了。
四、开始⾃定义回复
注释掉$wechatObj->valid(); 这⾏,同时去掉//$wechatObj->responseMsg();这⾏的注释。
你可以修改responseMsg函数⾥⾯的代码,根据⽤户的消息类型('text','image','location')和消息内容来回复⽤户不同的内容。消息接⼝就可以使⽤了,发个消息试试看吧?
1.封装weixin.class.php
由于公众平台的通信使⽤的是特定格式的XML数据,每次接受和回复都要去做⼀⼤堆的数据处理。
我们就考虑在这个基础上做⼀次封装,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}
1
]>
%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['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()