appId = $appId; $this->appSecret = $appSecret; } /** * 获取微信用户信息 * @param $js_code * @return type */ public function get_user_info($js_code) { $url = 'https://api.weixin.qq.com/sns/jscode2session'; $data = array( 'appid' => $this->appId, 'secret' => $this->appSecret, 'js_code' => $js_code, 'grant_type' => 'authorization_code', ); $user_info = $this->sendGet($url, $data); return $user_info; } /** * 发送模板消息 * @param $openid * @param $form_id * @param $message_data * @return type */ public function set_message($openid, $form_id, $message_data) { $appname = \Common\Common\share\WxShareServer::APPNAME; $wx_share_server = new \Common\Common\share\WxShareServer($this->appId, $this->appSecret, $appname); $access_token = $wx_share_server->getAccessToken(); $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=" . $access_token; $data = array( 'touser' => $openid, 'template_id' => MINI_APP_SIGN_EVENT, 'form_id' => $form_id, 'data' => $message_data, ); $data_json = array(); $data_json['data'] = json_encode($data); $result = $this->sendPost($url, $data_json); return $result; } /** * 发送get请求 * @param type $url * @param type $data * @return type 失败返回false */ protected function sendGet($url, $data = null) { $status = false; if (empty($url)) { echo '亲,URL都没有,你要GET到哪去?'; $status = false; return $status; } if (!empty($data)) { $get_data = ''; foreach ($data as $k => $v) { $get_data .= "{$k}={$v}&"; } $get_data = trim($get_data, '&'); } if (!empty($get_data)) { $url .= '?' . $get_data; } $result = file_get_contents($url); if (!empty($result)) { $result = $this->jsonDecode($result); } return $result; } /** * * @param type $json * @return boolean */ protected function jsonDecode($json) { if (empty($json)) { echo '亲,json都没有,你想解析个蛋?'; $status = false; return $status; } $json = strval($json); $arr = json_decode(trim($json, chr(239) . chr(187) . chr(191)), true); //删除bom头 return $arr; } /** * 发送post请求 * @param type $url * @param type $data * @return type */ protected function sendPost($url, $data = null) { $post_data = implode('&', $data); $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); ob_start(); curl_exec($ch); $result = ob_get_contents(); ob_end_clean(); return $result; } }