您的位置:首页 > 编程语言 > PHP开发

php yii框架实现微信JSSDK 附近位置信息 语音图片回复

2017-10-24 19:42 477 查看
FaceController.php
<?phpnamespace frontend\controllers;use Yii;use yii\web\Controller;/*** Default controller for the `jzw` module*/class FaceController extends Controller{public $enableCsrfValidation = false;public function actionInit(){// echo $_GET["echostr"];die;$postStr = file_get_contents("php://input");file_put_contents("./log/".time().".txt",$postStr);if (!empty($postStr)) {$postObj = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);$type = $postObj['MsgType'];switch ($type){case "event":switch ($postObj['Event']){case "subscribe":$resultStr = $this->actionFsend($postObj,"欢迎!");break;}break;case "text":switch ($postObj['Content']){case "酒店":$resultStr = $this->actionFujin($postObj,$postObj['Content']);break;case "足疗":$resultStr = $this->actionFujin($postObj,$postObj['Content']);break;case "学校":$resultStr = $this->actionFujin($postObj,$postObj['Content']);break;default:$resultStr = $this->actionFsend($postObj,"什么东东??");break;}break;case "location":$contentStr  = "纬度 ".$postObj['Location_X']." 经度".$postObj['Location_Y'];$resultStr = $this->actionFsend($postObj,$contentStr);$this->actionRuku($postObj['Location_X'],$postObj['Location_Y']);break;case "image":// $imagurl = "http://jiaoss.ngrok.cc/advanced/frontend/web/images/30.png";$resultStr = $this->actionImg($postObj);break;case "voice":// $imagurl = "http://jiaoss.ngrok.cc/advanced/frontend/web/images/30.png";$resultStr = $this->actionViod($postObj);break;default:$resultStr = "";break;}echo $resultStr;}}public function actionFujin($postObj,$con){$location = "40.043471,116.302036";$url = 'http://api.map.baidu.com/place/v2/search';$data = ["query" => $con,"output" => "json","scope" => 2,"filter" => "sort_name:distance","ak" => "AsVItyAbrleKZdfdP5dcDIx7We0hi9g8",//百度自己的ak"location" => $location,"radius" => 8000,];$url = $url . "$method?".http_build_query($data);$info = json_decode(file_get_contents($url),true);$result = $info['results'];// var_dump($data);die;$str = array();for ($i = 0; $i < count($result); $i++) {$str[] = array("Title"=>"名称:".$result[$i]['name'].","." 距离我:".$result[$i]['detail_info']['distance']."米\r\n",);}if (count($str) == 0) {$content = "附近没有".$con;$this->actionFsend($postObj,$content);}else{$con = '';foreach ($str as $key => $val) {$con .=  $val['Title'];}//文本$this->actionFsend($postObj,$con);}}/*** [JS-SDK]* @param  [type] $type    [description]* @return [type]          [description]*/public function actionJssdk(){$token = $this->actionToken();$url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$token.'&type=jsapi';$res = $this->actionPost_curl($url);// var_dump($res);die;$ticket = json_decode($res,1);$ticket = $ticket['ticket'];$timestamp = time();// var_dump($ticket);die;$urls = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];$noncestr = 'wedfgxdfgdsert';// $data = [//       'jsapi_ticket' => $ticket,//       'noncestr' => $noncestr,//       'timestamp' => $timestamp,//       'url' => $urls// ];$string = "jsapi_ticket=$ticket&noncestr=$noncestr×tamp=$timestamp&url=$urls";// 这里参数的顺序要按照 key 值 ASCII 码升序排序// $string = http_build_query($data);// var_dump($stbdbdring);die;$signature = sha1($string);// var_dump($signature);die;$info = ["timestamp" => $timestamp,"nonceStr"  => $noncestr,"signature" => $signature,];return $this->render('index',['info'=>$info]);}/*** [模板消息]* @param  [type] $type    [description]* @return [type]          [description]*/public function actionMoban(){$token = $this->actionToken();$url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$token;$data =  '{"touser":"obtn_0frALfP8Z10gppPeezyHPLo","template_id":"ZumNvM6SkNfH5MbO-YJj313A4V4W2jjVRYgIsSiRGYY","data":{"goodsname": {"value":"恭喜你购买成功!","color":"#173177"},"money": {"value":"500w","color":"#173177"},"time": {"value":"22","color":"#173177"},"user": {"value":"xiao","color":"#173177"}}}';$strr = $this->actionPost_curl($url,$data);file_put_contents("./log/moban".time().".txt",$strr);echo $strr;}/*** [图片回复]* @param  [type] $type    [description]* @return [type]          [description]*/public function actionImg($type){$con =  "<xml><ToUserName><![CDATA[".$type['FromUserName']."]]></ToUserName><FromUserName><![CDATA[".$type['ToUserName']."]]></FromUserName><CreateTime>".time()."</CreateTime><MsgType><![CDATA[image]]></MsgType><Image><MediaId><![CDATA[".$type['MediaId']."]]></MediaId></Image></xml>";// $str = sprintf($con,$iurl);file_put_contents("./log/img".time().".txt",$con);echo $con;}/*** [文本回复]* @param  [type] $type    [description]* @return [type]          [description]*/public function actionFsend($type,$content){$con =  "<xml><ToUserName><![CDATA[".$type['FromUserName']."]]></ToUserName><FromUserName><![CDATA[".$type['ToUserName']."]]></FromUserName><CreateTime>".time()."</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[".$content."]]></Content></xml>";file_put_contents("./log/wen".time().".txt",$con);echo $con;}/*** [语音回复]* @param  [type] $type    [description]* @return [type]          [description]*/public function actionViod($type){$con =  "<xml><ToUserName><![CDATA[".$type['FromUserName']."]]></ToUserName><FromUserName><![CDATA[".$type['ToUserName']."]]></FromUserName><CreateTime>".time()."</CreateTime><MsgType><![CDATA[voice]]></MsgType><Voice><MediaId><![CDATA[".$type['MediaId']."]]></MediaId></Voice></xml>";file_put_contents("./log/void".time().".txt",$con);echo $con;}public function actionRuku($jing,$wei){$res = Yii::$app->db->createCommand()->insert('lihai', ['jing' => $jing,'wei' => $wei,])->execute();if ($res) {$resultStr = $this->actionFsend($postObj,"ok!");}else{echo 2;}}// //   }Public function actionToken(){$file="./log/token.txt";// echo $file;die;if(is_file($file)){$access_token=file_get_contents($file);$access_token=json_decode($access_token,true);if(!$access_token['access_token'] || time()-$access_token['time']>7200){$access_token_url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx72ac4b07e5d446cb&secret=4121059b6f95a473b23cb6ecc52dcfb2";$access_token=file_get_contents($access_token_url);$access_token=json_decode($access_token,true);$access_token['time']=time();file_put_contents($file,json_encode($access_token));}}return $access_token['access_token'];}public function actionPost_curl($linch,$post_data = null){$curl = curl_init();//var_dump($curl);diecurl_setopt($curl,CURLOPT_URL,$linch);curl_setopt($curl,CURLOPT_HEADER,0);curl_setopt($curl,CURLOPT_NOBODY,0);curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,0);//1验证https  0不验证if (!empty($post_data)) {curl_setopt($curl, CURLOPT_POST, 1);curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);}$info = curl_exec($curl);// var_dump($info);curl_close($curl);return $info;// file_put_contents("./vo".time().".txt",$info);}}
JSSDK    index.php<script type="text/javascript" src='http://res.wx.qq.com/open/js/jweixin-1.2.0.js'></script><script type="text/javascript">wx.config({debug: true,appId: 'wx72ac4b07e5d446cb',timestamp: <?=$info["timestamp"];?>,nonceStr: '<?=$info["nonceStr"];?>',signature: '<?=$info["signature"];?>',jsApiList: ['checkJsApi','onMenuShareQQ','onMenuShareQZone','onMenuShareAppMessage','onMenuShareTimeline']});wx.ready(function(){wx.onMenuShareQQ({title: '下雪了!', // 分享标题desc: '今年北京积雪又创新高', // 分享描述link: 'http://www.baidu.com/', // 分享链接imgUrl: 'http://jzwss.ngrok.cc/advanced/frontend/web/images/30.png', // 分享图标success: function () {// 用户确认分享后执行的回调函数alert('已成功');},cancel: function () {// 用户取消分享后执行的回调函数alert('已取消');}});wx.onMenuShareAppMessage({title: '下雪了!', // 分享标题desc: '今年北京积雪又创新高', // 分享描述link: 'http://jzwss.ngrok.cc/shouquan.php', // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致imgUrl: 'http://jzwss.ngrok.cc/advanced/frontend/web/images/30.png', // 分享图标type: '', // 分享类型,music、video或link,不填默认为linkdataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空success: function () {// 用户确认分享后执行的回调函数alert('已成功');},cancel: function () {// 用户取消分享后执行的回调函数}});wx.onMenuShareTimeline({title: '下雪了!', // 分享标题link: 'http://jzwss.ngrok.cc/shouquan.php', // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致imgUrl: 'http://jzwss.ngrok.cc/advanced/frontend/web/images/002.jpg', // 分享图标success: function () {// 用户确认分享后执行的回调函数},cancel: function () {// 用户取消分享后执行的回调函数}});wx.onMenuShareQZone({title: '下雪了!', // 分享标题desc: '今年北京积雪又创新高', // 分享描述link: 'http://jzwss.ngrok.cc/shouquan.php', // 分享链接imgUrl: 'http://jzwss.ngrok.cc/advanced/frontend/web/images/001.jpg', // 分享图标success: function () {// 用户确认分享后执行的回调函数},cancel: function () {// 用户取消分享后执行的回调函数}});});</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: