THINKPHP5微信模板消息推送跨坑记录。

https://blog.csdn.net/qd55815634/article/details/79043430

上面用的是CURL推送,推送少还可以,但是推送多了。就直接超时了。

获取所以的关注用户的函数(不懂的参数可以查看微信文档的说明):

function get_allopenid($access_token,$next_openid = ''){
		$url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token={$access_token}&next_openid=".$next_openid;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
		$data = curl_exec($ch);
		$data = json_decode($data,true);
		if ($data["count"] == 10000){
			$newdata = $this->get_allopenid($access_token,$data["next_openid"]);
			$data["data"]["openid"] = array_merge_recursive($date["data"]["openid"], $newdata["data"]["openid"]);
		}
		return $data;
	}

模版信息组装函数:

function json_tempalte($openid,$data,$template_id = 'DtlkLjhNf5p5P7kYorbm-6kop3zlSyct1wL70fuJ8ME'){
		$template=array(
			'touser'=>$openid,//openID
			'template_id'=>$template_id,//模版id
			'url'=>$data['url'],
			'topcolor'=>"#7B68EE",
			'data'=>array(
				'first'=>array('value'=>$data['first'],'color'=>"#000"),
				'keyword1'=>array('value'=>$data['keyword1'],'color'=>'#F70997'),
				'keyword2'=>array('value'=>$data['keyword2'],'color'=>'#248d24'),
				'keyword3'=>array('value'=>date("Y-m-d H:i:s"),'color'=>'#000'),
				'remark'  =>array('value'=>$data['remark'],'color'=>'#1784e8'), )
		);//各个参数不明白的就去看文档,很详细。
		return $template;
	}

主要部分(写在你的访问方法里面的):

            $data = input('post.');//前端传过来的值,一般是模版消息的各个内容。
			$value = db('wx_config')->where([ 'key' => 'SHOPWCHAT'])->value('value');
			$value = json_decode($value,true);
			$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$value['appid']."&secret=".$value['appsecret'];

			if (cookie('access_token')){
				$access_token=cookie('access_token');
			}else{
				$cont = json_decode($this->getToken($url));
				$access_token=$cont->access_token;
				setcookie('access_token',$access_token,7200);
			}//缓存assesstoken
			$all = $this->get_allopenid($access_token);
			$count = $all['total'];
			$openids = $all['data']['openid'];

			foreach ($openids as $key => $value) {
				if (db('users')->where('openid',$value)->value('unsub') == '1'){
					unset($openids[$key]);
				}
			}
			$openids = array_values($openids);//这部分主要是拒绝推送的用户匹配一下重组数据。

			foreach ($openids as $key => $value) {//循环推送,一条一条的。我这里500条左右花了 21秒。
				$params = json_encode($this->json_tempalte($value,$data),JSON_UNESCAPED_UNICODE);
				$fp = fsockopen('api.weixin.qq.com', 80, $error, $errstr, 1);
				$http = "POST /cgi-bin/message/template/send?access_token={$access_token} HTTP/1.1\r\nHost: api.weixin.qq.com\r\nContent-type: application/x-www-form-urlencoded\r\nContent-Length: " . strlen($params) . "\r\nConnection:close\r\n\r\n$params\r\n\r\n";
				fwrite($fp, $http);
				fclose($fp);
			}
			return json(['code' => 1, 'msg' => '推送完成!', 'url' => url('pnotive/send')]);

以上。

异步推送请看下一篇。后来学会的。

猜你喜欢

转载自blog.csdn.net/qq_34876813/article/details/82590853