config('wx') 微信配置
composer require overtrue/wechat:~3.1 -vvv
1.首先打通微信公众号与网站之间的通道$app = new Application(\config('wx'));
// 从项目实例中得到服务端应用实例。
$server = $app->server;
$response = $server->serve();
//将得到的token直接返回回去
$response->send();
2.判定事件的类型
//订阅事件,返回模版消息
$server->setMessageHandler(function ($message) {
if($message->Event=="subscribe")
{
$app = new Application(\config('wx'));
$notice = $app->notice;
$userId = $message->FromUserName;
$templateId = 'dYhH-3dfMCwtjVXOM4Tt_WXKsrTLpJOLgBDmoy4vZQo';
$url = 'https://www.baidu.com';
$data = array(
"username" => $message->FromUserName,
);
$result = $notice->uses($templateId)->withUrl($url)->andData($data)->andReceiver($userId)->send
();
return;
// return $result;
}
//语音识别
if($message->MsgType=="voice"){
return $resultStr="别调戏我啊!!";
}
$weatherXml=file_get_contents("http://flash.weather.com.cn/wmaps/xml/sichuan.xml");
//4.2 解析天气XML
$citys=simplexml_load_string($weatherXml);
//$content=$_GET['keywords'];/**/
//ob_start();
$state = "未知城市";
// 发送美女排行版,返回美女排行榜
if($message->Content=="美女排行榜"){
// var_dump(12234442);
$girls = [
['Title'=>'1.杨洋',
'Description'=>'1.杨洋 565822演员 歌星 歌手',
'PicUrl'=>'http://www.hercity.com/data/upfiles/thumb/2011/11/20111130125300510834.jpg',
'Url'=>'http://mei.hercity.com/yangyang/',
],
['Title'=>'1.杨洋',
'Description'=>'1.杨洋 565822演员 歌星 歌手',
'PicUrl'=>'http://www.hercity.com/data/upfiles/thumb/2011/11/20111130125300510834.jpg',
'Url'=>'http://mei.hercity.com/yangyang/',
],
['Title'=>'1.杨洋',
'Description'=>'1.杨洋 565822演员 歌星 歌手',
'PicUrl'=>'http://www.hercity.com/data/upfiles/thumb/2011/11/20111130125300510834.jpg',
'Url'=>'http://mei.hercity.com/yangyang/',
],
['Title'=>'1.杨洋',
'Description'=>'1.杨洋 565822演员 歌星 歌手',
'PicUrl'=>'http://www.hercity.com/data/upfiles/thumb/2011/11/20111130125300510834.jpg',
'Url'=>'http://mei.hercity.com/yangyang/',
],
['Title'=>'1.杨洋',
'Description'=>'1.杨洋 565822演员 歌星 歌手',
'PicUrl'=>'http://www.hercity.com/data/upfiles/thumb/2011/11/20111130125300510834.jpg',
'Url'=>'http://mei.hercity.com/yangyang/',
]
];
$girl=[];
foreach ($girls as $value){
$girl[] = new News(
[ 'title' => $value['Title'],
'description' => $value['Description'],
'url' => $value['Url'],
'image' => $value['PicUrl'],]
);
}
// exit;
return $girl;
}
//发送热卖商品 弹出热卖商品
if($message->EventKey=="V1001_TODAY_MUSIC"||$message->Content=="热卖商品"){
$goods = Db::name("goods")->alias("a")->join('goods_intro b','a.id=b.id')->order('inputtime
desc')->limit(5)->select();
$new=[];
// 创建图文信息
foreach($goods as $good){
$new[] = new News(
[ 'title' => $good['name'],
'description' => $good['name'],
'url' => "http://www.wwja6.com/home/goods?id=".$good['id'],
'image' => $good['logo'],]
);
}
return $new;
}
// 发送帮助,返回帮助信息
if($message->Content==="帮助"){
return "联系电话 13999988888 \n 联系地址:互联网学院3A 2F 0830php";
}
if($message->Content==="解除绑定"){
$user = Db::name("user")->where(["open_id"=>$message->FromUserName])->find();
if($user&&$user = Db::name("user")->where(["id"=>$user['id']])->update(["open_id"=>""])){
return "解除绑定成功";
}else{
return "未绑定账号";
}
}
3.返回消息
$response = $server->serve();
//将得到的token直接返回回去
$response->send();