getJson($url); $token = json_decode($token, true); $uri ='https://api.weixin.qq.com/cgi-bin/message/template/send'; $access_token = $token["access_token"]; $uri = $uri.'?access_token='.$access_token; $data= array('touser'=>$openid, //发给谁 'template_id'=>'G_tFTFVPqY5krFuZ59MBybGENuLw22imaJgE7ilhwOU', //模板id 'url'=>'#', //这个是你发送了模板消息之后,当用户点击时跳转的连接 'topcolor'=>"#FF0000", //颜色 'miniprogram' => '', 'data'=>array( 'first'=>array( 'value'=>$data['first'], 'color'=>'#173177' ), 'keyword1'=>array( 'value'=>$data['course_name'], 'color'=>'#173177' ), 'keyword2'=>array( 'value'=>$data['teacher_name'], 'color'=>'#173177' ), 'keyword3'=>array( 'value'=>$data['tel'], 'color'=>'#173177' ), 'keyword4'=>array( 'value'=>$data['time'], 'color'=>'#173177' ), 'remark'=>array( 'value'=>$data['remark'], 'color'=>'#173177' ) ) ); $res_data = $this->getJson($uri,$data); $res_data = json_decode($res_data, true); if ($res_data['errcode'] != 0) { return false; } return true; } public function getJson($url = '', $param = [] ,$contentType = 'json'){ $ch = curl_init(); // 请求地址 curl_setopt($ch, CURLOPT_URL, $url); // 请求参数类型 $param = $contentType == 'json' ? urldecode(json_encode($param)) : http_build_query($param); // 关闭https验证 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // post提交 if($param){ curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $param); } // 返回的数据是否自动显示 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 执行并接收响应结果 $output = curl_exec($ch); // 关闭curl curl_close($ch); return $output !== false ? $output : false; } }