菜单

微信公众号信息回复

2018年11月16日 - Php

$template .=”</Articles>
</xml> “;
echo sprintf($template, $toUser, $fromUser, time(), ‘news’);

,而团结为什么没收受那么?

//代码如下

提示“该群众号暂时无法提供劳动,请稍后又试试”

解决办法:输入的返信息是success,但是不深受输出

   try {

   textMessage.setContent("success");

   response.setHeader("Content-type","text/html;charset=UTF-8");

        out = response.getWriter();
        //out.print(respMessage);  //次数是控制输出到前端
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        out.close();
        out = null;
    }
}

manbetx网页手机登录版 1manbetx网页手机登录版 2

manbetx网页手机登录版 3

 manbetx网页手机登录版 4

2.启用后文本信息乱码解决办法

public function __construct(){

3.己下了图灵机器人自动恢复,但是出现

manbetx网页手机登录版 5

 

}
}//class end

PrintWriter out = null;
try {
    response.setHeader("Content-type","text/html;charset=UTF-8");
    out = response.getWriter();
    out.print(respMessage);
} catch (IOException e) {
    e.printStackTrace();
} finally {
    out.close();
    out = null;
}

}
$template = “<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>”;
//注意模板被的中括号 不能够少 也非能够多
$fromUser = $postObj->ToUserName;
$toUser = $postObj->FromUserName;
$time = time();
// $content = ‘18723180099’;
$msgType = ‘text’;
echo sprintf($template, $toUser, $fromUser, $time, $msgType,
$content);

   
 最后发现凡是manbetx网页手机登录版 6未曾启动导致的。

 

     参照https://www.kancloud.cn/digest/wechat-java/123962
写了servlet并处理后,发现于服务器的
tomcat/logs/localhost_access.txt的日记中 总是接收不至
微信端发送的post的要,因为 微信的
文本消息回复原理是,用户发送信息被微信,微信将这些消息和用户信息经过post请求发送给服务器。服务器提供post接口的URL,这个URL就是在开发者文档里部署的URL。如下所示

function http_curl(){
//获取imooc
//1.初始化curl
$ch = curl_init();
$url = ‘http://www.baidu.com’;
//2.设置curl的参数
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//3.采集
$output = curl_exec($ch);
//4.关闭
curl_close($ch);
var_dump($output);
}

 

function getWxServerIp(){
$accessToken =
“6vOlKOh7r5uWk_ZPCl3DS36NEK93VIH9Q9tacreuxJ5WzcVc235w_9zONy75NoO11gC9P0o4FBVxwvDiEtsdX6ZRFR0Lfs_ymkb8Bf6kRfo”;
$url =
“https://api.weixin.qq.com/cgi-bin/getcallbackip?access\_token=".$accessToken;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$res = curl_exec($ch);
curl_close($ch);
if(curl_errno($ch)){
var_dump(curl_error($ch));
}
$arr = json_decode($res,true);
echo “<pre>”;
var_dump( $arr );
echo “</pre>”;

}

public function index(){
//获得参数 signature nonce token timestamp echostr
$nonce = $_GET[‘nonce’];
$token = ‘imooc’;
$timestamp = $_GET[‘timestamp’];
$echostr = $_GET[‘echostr’];
$signature = $_GET[‘signature’];
//形成往往组,然后按字典序排序
$array = array();
$array = array($nonce, $timestamp, $token);
sort($array);
//拼接成字符串,sha1加密 ,然后同signature进行校验
$str = sha1( implode( $array ) );
if( $str == $signature && $echostr ){
//第一差对接weixin api接口的上
echo $echostr;
exit;
}else{
$this->reponseMsg();
}
}
// 接收事件推送并恢复
public function reponseMsg(){
//1.获到微信推送过来post数据(xml格式)
$postArr = $GLOBALS[‘HTTP_RAW_POST_DATA’];
//2.处理消息类型,并安装回复类型及情节
/*<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[FromUser]]></FromUserName>
<CreateTime>123456789</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[subscribe]]></Event>
</xml>*/
$postObj = simplexml_load_string( $postArr );
//$postObj->ToUserName = ”;
//$postObj->FromUserName = ”;
//$postObj->CreateTime = ”;
//$postObj->MsgType = ”;
//$postObj->Event = ”;
// gh_e79a177814ed
//判断该数据包是否是订阅的轩然大波推送
if( strtolower( $postObj->MsgType) == ‘event’){
//如果是关注 subscribe 事件
if( strtolower($postObj->Event == ‘subscribe’) ){
//回复用户信息(纯文本格式)
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time = time();
$msgType = ‘text’;
$content =
‘欢迎关注我们的微信公众账号’.$postObj->FromUserName.’-‘.$postObj->ToUserName;
$template = “<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>”;
$info = sprintf($template, $toUser, $fromUser, $time, $msgType,
$content);
echo $info;
/*<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[你好]]></Content>
</xml>*/

//注意:进行多图文发送时,子图文个数不能够过10单
}else{
switch( trim($postObj->Content) ){
case 1:
$content = ‘您输入的数字是1’;
break;
case 2:
$content = ‘您输入的数字是2’;
break;
case 3:
$content = ‘您输入的数字是3’;
break;
case 4:
$content = “<a href=’http://www.imooc.com'&gt;慕课&lt;/a&gt;”;
break;
case ‘英文’:
$content = ‘imooc is ok’;
break;
}
$template = “<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>”;
//注意模板被之中括号 不克少 也不可知多
$fromUser = $postObj->ToUserName;
$toUser = $postObj->FromUserName;
$time = time();
// $content = ‘18723180099’;
$msgType = ‘text’;
echo sprintf($template, $toUser, $fromUser, $time, $msgType,
$content);

//当微信用户发送imooc,公众账号回复‘imooc is very good’
/*<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[你好]]></Content>
</xml>*/
/*if(strtolower($postObj->MsgType) == ‘text’){
switch( trim($postObj->Content) ){
case 1:
$content = ‘您输入的数字是1’;
break;
case 2:
$content = ‘您输入的数字是2’;
break;
case 3:
$content = ‘您输入的数字是3’;
break;
case 4:
$content = “<a href=’http://www.imooc.com'&gt;慕课&lt;/a&gt;”;
break;
case ‘英文’:
$content = ‘imooc is ok’;
break;

}
}
*/
//用户发送tuwen1关键字之时节,回复一个单图文
if( strtolower($postObj->MsgType) == ‘text’ &&
trim($postObj->Content)==’tuwen2′ ){
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$arr = array(
array(
‘title’=>’imooc’,
‘description’=>”imooc is very cool”,
‘picUrl’=>’http://www.imooc.com/static/img/common/logo.png’,
‘url’=>’http://www.imooc.com’,
),
array(
‘title’=>’hao123’,
‘description’=>”hao123 is very cool”,
‘picUrl’=>’https://www.baidu.com/img/bdlogo.png’,
‘url’=>’http://www.hao123.com’,
),
array(
‘title’=>’qq’,
‘description’=>”qq is very cool”,
‘picUrl’=>’http://www.imooc.com/static/img/common/logo.png’,
‘url’=>’http://www.qq.com’,
),
);
$template = “<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>”.count($arr).”</ArticleCount>
<Articles>”;
foreach($arr as $k=>$v){
$template .=”<item>
<Title><![CDATA[“.$v[‘title’].”]]></Title>
<Description><![CDATA[“.$v[‘description’].”]]></Description>
<PicUrl><![CDATA[“.$v[‘picUrl’].”]]></PicUrl>
<Url><![CDATA[“.$v[‘url’].”]]></Url>
</item>”;
}

 

}//if end
}//reponseMsg end

}
}

function getWxAccessToken(){
//1.请求url地址
$appid = ‘wx08d5c2cb632bb5e4’;
$appsecret = ’06d3444fb9abd8d00314eb4a38ad61a8′;
$url =
“https://api.weixin.qq.com/cgi-bin/token?grant\_type=client\_credential&appid=".$appid."&secret=".$appsecret;
//2初始化
$ch = curl_init();
//3.设置参数
curl_setopt($ch , CURLOPT_URL, $url);
curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
//4.调用接口
$res = curl_exec($ch);
//5.关闭curl
curl_close( $ch );
if( curl_errno($ch) ){
var_dump( curl_error($ch) );
}
$arr = json_decode($res, true);
var_dump( $arr );
}

manbetx网页手机登录版 7

manbetx网页手机登录版 8

<?php
class IndexAction extends Action {

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图