Java的微信开发中使用XML格式和JSON格式数据的详解及实例
2021-06-24
注释掉 $->();行,并取消注释 //$->();同时排队。
您可以修改函数中的代码微信公众平台开发 php,根据用户的消息类型('text'、''、'')和消息内容,以不同的内容回复用户。
消息界面可以使用了。发送消息并尝试一下?
1...php
由于微信公众平台的交流使用的是特定格式的XML数据,每次接收和回复都需要进行大量的数据处理。
我们会考虑在此基础上做一个打包微信公众平台开发 php,..php,代码如下:
代码如下:
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['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){
//这里是你记录调试信息的地方 请自行完善 以便中间调试
}
}
?>
2.call..php
在你的微信公众平台主界面文件中,修改代码如下:
代码如下:
getMsg();
$type = $weixin->msgtype;//消息类型
$username = $weixin->msg['FromUserName'];//哪个用户给你发的消息,这个$username是微信加密之后的,但是每个用户都是一一对应的
if ($type==='text') {
if ($weixin->msg['Content']=='Hello2BizUser') {//微信用户第一次关注你的账号的时候,你的公众账号就会受到一条内容为'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);
}
}elseif ($type==='location') {
//用户发送的是位置信息 稍后的文章中会处理
}elseif ($type==='image') {
//用户发送的是图片 稍后的文章中会处理
}elseif ($type==='voice') {
//用户发送的是声音 稍后的文章中会处理
}
$weixin->reply($reply);
?>
3.查询代码
还需要将数据库中的查询结果格式化成特定的形式
代码如下:
search($this->keyword);//普通的根据关键词查询数据库的操作 代码就不用分享了
if(is_array($list)&&!empty($list)){
foreach($list as $msg){
$record[]=array(//以下代码,将数据库中查询返回的数组格式化为微信返回消息能接收的数组形式,即title、description、picurl、url 详见微信官方的文档描述
'title' =>$msg['title'],
'description' =>$msg['discription'],
'picurl' => $msg['pic_url'],
'url' =>$msg['url']
);
}
}
return $record;
}
?>
以上是PHP对接微信公众平台消息接口的开发过程详解及开发过程示例。更多详情请关注php中文网其他相关文章!
免责声明:本文首发于php中文网。转载请注明出处。感谢您的尊重!如果您有任何问题,请联系我们
特别推荐:微信公众平台开发
上一篇:解决PHP微信开发数据缓存问题 下一篇:Java微信开发中使用XML格式和JSON格式数据详解及实例





