首頁 >php教程 >php手册 >教你用wechat类玩转微信公众号

教你用wechat类玩转微信公众号

WBOY
WBOY原創
2016-06-07 11:36:431518瀏覽

通过wechat类教你实现大部分常见功能,包括查天气,公交路线,机器人的自动回复等。。。记得点个赞哦!
没有公众号可以用的可以用自己的微信号申请一个测试号,功能还是挺全的<?php <br /> <br> namespace Home\Controller;<br> <br> use Think\Controller;<br> <br> <br> class IndexController extends Controller {<br> <br>     public function index() {<br>         $options = array(<br>             'token' => 'ruanqin', // 填写你设定的token<br>             'appid' => '1111111', // 填写高级调用功能的appid<br>             'appsecret' => '11111111' // 填写高级调用功能的密钥<br>         );<br> //这两个类我已放到附件,有需要可以下载<br>         Vendor('weixin.Wechat');<br>         Vendor('weixin.Snoopy');<br>         $weObj = new \Wechat($options);<br> <br>         $weObj->valid(); // 验证key<br>            //获取菜单操作:<br>        $menu = $weObj->getMenu();<br>           //设置菜单<br>        $newmenu =  array(<br>            <br>             'button' => array (<br>                   0 => array (<br>                         'name' => '扫码',<br>                         'sub_button' => array (<br>                             0 => array (<br>                               'type' => 'scancode_waitmsg',<br>                               'name' => '扫码带提示',<br>                               'key' => 'rselfmenu_0_0',<br>                         ),<br>                             1 => array (<br>                               'type' => 'scancode_push',<br>                               'name' => '扫码推事件',<br>                               'key' => 'rselfmenu_0_1',<br>                         ),<br>                     ),<br>                       ),<br>                   1 => array (<br>                     'name' => '发图',<br>                         'sub_button' => array (<br>                             0 => array (<br>                               'type' => 'pic_sysphoto',<br>                               'name' => '系统拍照发图',<br>                               'key' => 'rselfmenu_1_0',<br>                             ),<br>                             1 => array (<br>                               'type' => 'pic_photo_or_album',<br>                               'name' => '拍照或者相册发图',<br>                               'key' => 'rselfmenu_1_1',<br>                             ),<br>                              2 => array (<br>                             'type' => 'view',<br>                             'name' => 'baidu',<br>                             'url' => 'http://www.baidu.com',<br>                             'key' => 'rselfmenu_1_2',<br>                             ),<br>                     ),<br>                       ),<br>                       2 => array (<br>                         'type' => 'location_select',<br>                         'name' => '发送位置',<br>                         'key' => 'rselfmenu_2_0'<br>                       ),<br>                  <br>                     ),<br>          <br>           );<br>    $result = $weObj->createMenu($newmenu);<br> <br> //获得用户发送过来的消息的类型,有"text","music","image"等<br> $type = $weObj->getRev()->getRevType();<br> switch($type) {<br>     case \Wechat::MSGTYPE_TEXT:<br>             //获得用户发送过来的文字消息内容<br>             $content=$weObj->getRev()->getRevContent();<br>             //从消息的结尾数第二个字开始截取,截取两个字<br>             $str = mb_substr($content,-2,2,"UTF-8");<br>             //从消息的开头开始,截掉末尾的两个字,便得关键字。<br>             $str_key = mb_substr($content,0,-2,"UTF-8");<br>             //然后加以判断是否为关键字,是否为空,符合要求则调用函数查询翻译数据<br>             if($str == '翻译' && !empty($str_key)) {<br>                 $url1="http://openapi.baidu.com/public/2.0/bmt/translate?client_id=9peNkh97N6B9GGj9zBke9tGQ&q={$str_key}&from=auto&to=auto";//百度翻译地址<br>                 //实例化采集类<br>                 $spy=new \Snoopy;<br>                 //获取采集来的数据<br>                 $spy->fetch($url1);<br>                 //将结果赋给$con_json<br>                 $con_json=$spy->results;<br>                 //json解析,转换为object对象类型<br>                 $transon=json_decode($con_json);<br>                 //读取翻译内容<br>                 $con_str = $transon->trans_result[0]->dst;<br>                 //以文字的形式输出结果<br>                 $weObj->text("{$con_str}")->reply();<br>             }<br>             //判断是否为关键字,是否为空,符合要求则调用函数查询书本数据<br>             if($str=='书本' && !empty($str_key)) {                              <br>                 $url="http://222.206.65.12/opac/search_rss.php?dept=ALL&title={$str_key}&doctype=ALL&lang_code=ALL&match_flag=forward&displaypg=20&showmode=list&orderby=DESC&sort=CATA_DATE&onlylendable=no";<br>                 $spp=new \Snoopy;<br>                 $spp->fetch($url);<br>                 $fa=$spp->results;<br>                 //将采集获取的XML数据转换成object对象类型<br>                 $f=simplexml_load_string($fa);<br>                 $da1=$f->channel->item[0]->title;<br>                 $da2=$f->channel->item[1]->title;<br>                $da3=$f->channel->item[2]->title;      <br>                $weObj->text("{$da1}\n{$da2}\n{$da3}")->reply();<br>             }<br>                //判断公交路线<br>                  if($str=='公交' && !empty($str_key)){<br>                     <br>                   $strbus=explode(',',$str_key);<br>                  $ch = curl_init();<br>                 $url = "http://apis.baidu.com/apistore/bustransport/buslines?city={$strbus[0]}&busNo={$strbus[1]}";<br>                 $header = array(<br>                     'apikey: 这里的apikey自己去百度APIstore去注册,免费的!' <br>           );<br>                 // 添加apikey到header<br>                 curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);<br>                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br>                 // 执行HTTP请求<br>                 curl_setopt($ch , CURLOPT_URL , $url);<br>                 $res = curl_exec($ch);<br>             <br>                $b=json_decode($res);<br>                $a=$b->retData->result;<br>                $c=simplexml_load_string($a);<br>                $d=$c->lines->line[1]->stats;<br>                $rq=explode(';',$d);<br>                $ww=implode('->',$rq);<br>                $weObj->text("{$ww}")->reply();<br>                 <br>                 }<br>              //景点查询<br>      <br>                  if($str=='景点' && !empty($str_key)){<br>                     <br>                  <br>                 $ch = curl_init();<br>                      $url = "http://apis.baidu.com/apistore/attractions/spot?id={$str_key}&output=json";<br>                 $header = array(<br>                     'apikey: <b>同上</b>',<br>                 );<br>                 // 添加apikey到header<br>                 curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);<br>                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br>                 // 执行HTTP请求<br>                 curl_setopt($ch , CURLOPT_URL , $url);<br>                 $res = curl_exec($ch);<br>             <br>                $a=json_decode($res);<br>                $name=$a->result->name;<br>                $phone=$a->result->telephone; <br>                $abstract=$a->result->abstract; <br>                $description=$a->result->description; <br>                $price=$a->result->ticket_info->price;<br>                $open_time=$a->result->ticket_info->open_time; <br>                      $b="景点名:{$name}\n景点联系电话:{$phone}\n价格:{$price}\n开放时间:{$open_time}\n景点详情:{$abstract}{$description}";<br>                 $weObj->text("{$b}")->reply();     <br>                 <br>                 }<br>   //这里做了一个健康的资讯信息查询,程序员嘛健康还是挺重要的<br>                 if($str=='健康' && !empty($str_key)){<br>                     $url = 'http://apis.baidu.com/yi18/lore/loreclass?id=1';<br>                     $header = array(<br>                         'apikey: 。。。自己申请。。。。。',<br>                     ); <br>                     $thisres=$this->curl($url,$header);<br>                    $a=$this->curl($url,$header);<br>                    $thisres=$a->yi18;  <br>                     for ($i=0; $i                          $q=$thisres[$i]->id;<br>                         $w=$thisres[$i]->name;<br>                         $qq.='';<br>                         $qq.="序号:{$q}--标题:{$w}\n";<br>                     }<br>                      $weObj->text("{$qq}\n请选择您想了解的信息的序号,如:1标题")->reply();<br>                 <br>                 <br>                 }<br>                    if($str=='标题' && !empty($str_key)){<br>                       $ch = curl_init();<br>                        $url = "http://apis.baidu.com/yi18/lore/list?page=1&limit=10&type=id&id={$str_key}";<br>                         $header = array(<br>                             'apikey: 同上',<br>                         ); <br>                         curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);<br>                         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br>                         // 执行HTTP请求<br>                         curl_setopt($ch , CURLOPT_URL , $url);<br>                         $res = curl_exec($ch);<br>                         $b=json_decode($res);<br>                        <br>                          $thisres2=$b->yi18; <br>                          foreach ($thisres2 as $key => $value) {<br>                                         $q=$thisres2[$key]->id;<br>                                         $w=$thisres2[$key]->title;<br>                                         $qq.='';<br>                                         $qq.="id:{$q}--信息:{$w}\n";<br>                           } <br>                                     <br>                      $weObj->text("{$qq}\n请选择您想了解的信息的id,如:18069信息")->reply();<br>                     <br>                     }<br>                     if($str=='信息' && !empty($str_key)){<br>                     <br>                     $url = "http://apis.baidu.com/yi18/lore/detail?id={$str_key}";<br>                     $header = array(<br>                         'apikey: ',<br>                     ); <br>                     $a=$this->curl($url,$header);<br>                      $thisres=$a->yi18; <br>          <br>                         $q=$thisres->message;<br>                         $w=$thisres->title;<br>                         //$qq='标题.$w.正文.$q';<br>                       <br>                         <br>                         $qq="标题:{$w}\n正文:{$q}";<br>                         $str=str_replace("<strong>","",$qq);<br>                         $ww=strip_tags($str);<br>                         $weObj->text("{$ww}")->reply();<br>                     <br>                     <br>                     }<br>                                             <br> <br>     <br>             //天气查询<br>             if($str == '天气' && !empty($str_key)) {<br>                 $url="http://api.map.baidu.com/telematics/v2/weather?location={$str_key}&ak=同上";<br>                 $sp=new \Snoopy;<br>                 $sp->fetch($url);<br>                 $l_xml=$sp->results;<br>                 $f=simplexml_load_string($l_xml);<br>                 $city=$f->currentCity;<br>                 $da1=$f->results->result[0]->date;<br>                 $da2=$f->results->result[1]->date;<br>                $da3=$f->results->result[2]->date;     <br>                 $w1=$f->results->result[0]->weather;<br>                 $w2=$f->results->result[1]->weather;<br>                $w3=$f->results->result[2]->weather;       <br>                 $p1=$f->results->result[0]->wind;<br>                 $p2=$f->results->result[1]->wind;<br>                $p3=$f->results->result[2]->wind;<br>                 $q1=$f->results->result[0]->temperature;<br>                 $q2=$f->results->result[1]->temperature;<br>                $q3=$f->results->result[2]->temperature;<br>                $k1=$f->results->result[0]->dayPictureUrl; <br>                $k2=$f->results->result[1]->dayPictureUrl;<br>                $k3=$f->results->result[2]->dayPictureUrl;    <br>                 $d1=$city.$da1.$w1.$p1.$q1;<br>                 $d2=$city.$da2.$w2.$p2.$q2;<br>                 $d3=$city.$da3.$w3.$p3.$q3;<br>                 $weObj->text("{$d1}\n{$d2}\n{$d3}")->reply();<br>             }<br> <br>             //剩下的任务就交给机器人自己去完成吧!;<br>            //这里我使用的是图灵机器人,通过我女朋友和机器人聊天实验表明还是付费的机器人比较聪明,免费的有点傻傻的,所以土豪们可以选择付费的,按条数付费的<br>             <br>             else {<br>              <br>                  $strurl="http://www.tuling123.com/openapi/api?key=自己申请个key吧&info={$content}";<br>                 $xhy=new \Snoopy;<br>                 $xhy->fetch($strurl);<br>                 $x_json=$xhy->results;<br>                 $strjson=json_decode($x_json);<br>                 //$a=var_dump($strjson);<br>                 $contentStr = $strjson->text;<br>                 //$weObj->text("{$contentStr}")->reply();<br>                 $weObj->text("{$contentStr}")->reply();<br>             }<br>             break;<br> <br>     case \Wechat::MSGTYPE_LOCATION:<br>                 //接收消息的地理位置<br>                 $arr1=$weObj->getRev()->getRevGeo();<br>                 $snoopy=new \Snoopy;<br>                 $url="http://api.map.baidu.com/telematics/v2/distance?waypoints=填你的位置的经纬度;{$arr1['x']},{$arr1['y']}&ak=同上";<br>                 $snoopy->fetch($url);<br>                 $lines_string=$snoopy->results; <br>                 $fk=simplexml_load_string($lines_string);<br>                 $juli=$fk->results->distance;<br>                 $contentstring="你和我的距离有{$juli}米远";<br>                 $weObj->text("{$contentstring}")->reply();<br>             break;<br>     //接受图片回复文字,也可以回复图片,你们自由发挥吧<br>     case \Wechat::MSGTYPE_IMAGE:<br>                 $b=" ";<br>                 $a=rand(1,3);<br>                  switch ($a)<br>                  {case 1;<br>                  $b="你傻逼啊,发这么二的图片";<br>                  break;<br>                  case 2;<br>                  $b="跟你一样丑";<br>                  break;<br>                  default;<br>                  $b="啊,我的天哪";    <br>                  }                                                                       <br>                 $weObj->text("哈哈我知道这是图片\n:{$b}")->reply();<br>                 //exit; <br>             break;<br>     //实现首次关注回复功能<br>     case \Wechat::MSGTYPE_EVENT:<br>             $msgEvent=$weObj->getRev()->getRevEvent();<br>            <br>             $weObj->text("感谢您关注阮琴专用测试版公众号\n查天气:城市+天气,如广州天气\n翻译:字词+翻译,如好翻译\n测距:发送位置\n查书:书名+书本,如php书本\n听歌:回复音乐\n查公交路线,如杭州,151公交\n健康知识:任意字+健康,如,查健康\n景点查询:景点名(请使用拼音)+景点如xihu景点\n还可选择发送图片,搞笑也会来和大家聊天哦!\n更多内容,敬请期待...")<br>                    ->reply();<br>             break;<br>     <br>     default:<br>             $weObj->text("查天气:城市+天气,如广州天气\n翻译:字词+翻译,如好翻译\n测距:发送位置\n查书:书名+书本,如php书本\n听歌:回复音乐\n还可选择发送图片,谢谢你的关注,更多内容,敬请期待...")->reply();<br>  <br> }<br>     }<br>    <br> }     </strong>

附件 weixin.zip ( 20.27 KB 下载:203 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn