开发公众号


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();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值