微信开发之附近区域信息 模板消息

<?php

namespace frontend\controllers;
use Yii;
use yii\web\Controller;

/**
 * Default controller for the `jzw` module
 */
//前提先配置好自己的URL配置信息,需要百度地图ak
class FaceController extends Controller
{
	public $enableCsrfValidation = false;

		public function actionInit()
    {

		$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",
    		"location" => $location,
    		"radius" => 8000,
    	];

    	$url = $url . "$method?".http_build_query($data);	
    	// file_put_contents("./a222.txt",$url);
    	$info = json_decode(file_get_contents($url),true);
    	// file_put_contents("./a2233.txt",file_get_contents($url));

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

	    }

	}
	/**
    * [模板消息的 发送]
    * @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);die
	    curl_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);			
		
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值