欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php 微信公众号图文消息回复的实现 与access_token

程序员文章站 2023-09-28 15:18:13
//代码如下

php 微信公众号图文消息回复的实现 与access_token

php 微信公众号图文消息回复的实现 与access_token

php 微信公众号图文消息回复的实现 与access_tokenphp 微信公众号图文消息回复的实现 与access_token

php 微信公众号图文消息回复的实现 与access_token

 

 php 微信公众号图文消息回复的实现 与access_token

 

//代码如下

<?php
class indexaction extends action {

public function __construct(){

}

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>*/

}
}

//当微信用户发送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'>慕课</a>";
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);

}
}
*/
//用户发送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>";
}

$template .="</articles>
</xml> ";
echo sprintf($template, $touser, $fromuser, time(), 'news');

//注意:进行多图文发送时,子图文个数不能超过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'>慕课</a>";
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);

}//if end
}//reponsemsg end

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 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 );
}

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>";


}
}//class end