access_token = WeixinBackAccount::getAccessToken(); } /** * * 创建临时二维码 * @param int $scene_id * @return ResultWrapper */ public function createTempQrcode($scene_id) { $ticketResult = $this->createTicket('QR_SCENE', $scene_id); if (!$ticketResult->isSuccess()) { return ResultWrapper::fail($ticketResult->getData()); } $result = $ticketResult->getData(); $qrcode = $this->exchangeQrcode($result['ticket']); return ResultWrapper::success($qrcode); } /** * * 创建永久二维码 * @param int $scene_id * @return ResultWrapper */ public function createSteadyQrcode($scene_id) { $ticketResult = $this->createTicket('QR_LIMIT_SCENE', $scene_id); if (!$ticketResult->isSuccess()) { return ResultWrapper::fail($ticketResult->getData()); } $result = $ticketResult->getData(); $qrcode = $this->exchangeQrcode($result['ticket']); return ResultWrapper::success($qrcode); } /** * * 创建二维码ticket * @param string $action_name * @param int $scene_id * @return ResultWrapper */ public function createTicket($action_name, $scene_id) { if (!$action_name) { return ResultWrapper::fail('无效 action_name 参数'); } if (!Verify::unsignedInt($scene_id)) { return ResultWrapper::fail('无效 scene_id 参数'); } $params = array( 'action_name' => $action_name, 'action_info' => array( 'scene' => array( 'scene_id' => $scene_id, ), ), ); if ($action_name == 'QR_SCENE') { $params['expire_seconds'] = DWeixinApi::$expire_seconds; } $result = $this->postCurl(DWeixinApi::$create_ticket_api, $params); if (!$result->isSuccess()) { return ResultWrapper::fail($result->getData()); } return ResultWrapper::success($result->getData()); } /** * * 通过ticket换取二维码地址 * @param string $ticket * @return boolean|string */ public function exchangeQrcode($ticket) { if (!$ticket) { return false; } $args = array( 'ticket' => urlencode($ticket), ); return DWeixinApi::$exchange_qrcode_api . '?ticket=' . urlencode($ticket); } /** * * 查询所有分组 * @return ResultWrapper */ public function searchAllGroups() { $result = $this->getCurl(DWeixinApi::$search_all_groups); if (!$result->isSuccess()) { return ResultWrapper::fail($result->getData()); } return ResultWrapper::success($result->getData()); } /** * * 创建分组 * @param string $groupName * @return ResultWrapper */ public function createGroup($groupName) { if (!$groupName) { return ResultWrapper::fail('无效组名'); } $params = array( 'group' => array('name' => $groupName), ); $result = $this->postCurl(DWeixinApi::$create_group, $params); if (!$result->isSuccess()) { return ResultWrapper::fail($result->getData()); } return ResultWrapper::success($result->getData()); } /** * * 查询用户所在分组 * @param string $openid * @return ResultWrapper */ public function searchMemberGroupid($openid) { if (!$openid) { return ResultWrapper::fail('无效openid'); } $params = array( 'openid' => $openid, ); $result = $this->postCurl(DWeixinApi::$search_member_groupid, $params); if (!$result->isSuccess()) { return ResultWrapper::fail($result->getData()); } return ResultWrapper::success($result->getData()); } /** * * 修改分组名 * @param int $groupid * @param string $groupName * @return ResultWrapper */ public function updateGroupName($groupid, $groupName) { if (!Verify::unsignedInt($groupid)) { return ResultWrapper::fail('无效分组id'); } if (!$groupName) { return ResultWrapper::fail('无效组名'); } $params = array( 'group' => array( 'id' => $groupid, 'name' => $groupName, ), ); $result = $this->postCurl(DWeixinApi::$update_group_name, $params); if (!$result->isSuccess()) { return ResultWrapper::fail($result->getData()); } return ResultWrapper::success($result->getData()); } /** * * 更新用户分组 * @param string $openid * @param int $to_groupid * @return ResultWrapper */ public function updateMemberGroup($openid, $to_groupid) { if (!$openid) { return ResultWrapper::fail('无效openid'); } if (!Verify::unsignedInt($to_groupid)) { return ResultWrapper::fail('无效分组id'); } $params = array( 'openid' => $openid, 'to_groupid' => $to_groupid, ); $result = $this->postCurl(DWeixinApi::$update_member_group, $params); if (!$result->isSuccess()) { return ResultWrapper::fail($result->getData()); } return ResultWrapper::success($result->getData()); } /** * * 创建菜单 * @param array $params * @return ResultWrapper */ public function createMenu($params) { $result = $this->postCurl(DWeixinApi::$create_menu, $params); if (!$result->isSuccess()) { return ResultWrapper::fail($result->getData()); } return ResultWrapper::success($result->getData()); } /** * * 删除菜单 */ public function deleteMenu() { $result = $this->getCurl(DWeixinApi::$delete_menu); if (!$result->isSuccess()) { return ResultWrapper::fail($result->getData()); } return ResultWrapper::success($result->getData()); } /** * 获取用户基本信息 * @param string $openid * @return ResultWrapper */ public function getUserInfo($openid) { if (!$openid) { return ResultWrapper::fail('无效openid'); } $params = array( 'openid' => $openid, 'lang' => 'zh_CN', ); $url = String::jointUrl(DWeixinApi::$get_user_info, $params); $result = $this->getCurl($url); if (!$result->isSuccess()) { return ResultWrapper::fail($result->getData()); } return ResultWrapper::success($result->getData()); } /** * 获取关注者列表 * @param string $next_openid * @return ResultWrapper */ public function getsOpenids($next_openid = null) { $params = array(); if (!is_null($next_openid) && $next_openid) { $params['next_openid'] = $next_openid; } $url = String::jointUrl(DWeixinApi::$gets_openids_list, $params); $result = $this->getCurl($url); if (!$result->isSuccess()) { return ResultWrapper::fail($result->getData()); } return ResultWrapper::success($result->getData()); } /** * 上传多媒体文件 * @param string $filePath * @param string $fileType * @return ResultWrapper */ public function mediaUpload($filePath, $fileType) { if (!$filePath) { return ResultWrapper::fail('无效文件路径'); } if (!in_array($fileType, array('image', 'voice', 'video', 'thumb'))) { return ResultWrapper::fail("无效文件类型:{$fileType}"); } $params = array( 'media' => "@{$filePath}", ); $url = String::jointUrl(DWeixinApi::$mediaUpload, array('access_token' => $this->access_token, 'type' => $fileType)); $ch = curl_init($url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $jsonResult = curl_exec($ch); curl_close($ch); if (!$jsonResult) { return ResultWrapper::fail('接口未返回数据'); } $result = json_decode($jsonResult, true); if ($result['errcode'] != 0) { return ResultWrapper::fail($jsonResult); } return ResultWrapper::success($result); } /** * 发送客服消息之:文本消息 * @param string @openid * @param string $content */ public function sendTextMessage($openid, $content) { if (!$openid) { return ResultWrapper::fail('无效openid'); } if (!$content) { return ResultWrapper::fail('消息内容不能为空'); } $params = array( 'touser' => $openid, 'msgtype' => 'text', 'text' => array( 'content' => $content, ), ); $result = $this->postCurl(DWeixinApi::$sendMessage, $params); if (!$result->isSuccess()) { return ResultWrapper::fail($result->getData()); } return ResultWrapper::success($result->getData()); } /** * 发送模板消息 * @param unknown $openid */ public function sendTemplateMessage($openid) { if (!$openid) { return ResultWrapper::fail('无效openid'); } $params = array( 'touser' => $openid, 'template_id' => 'DhQvR71fIoV91k0XdboZlT2lbue8GpNKUaz8xT8A1Jk', 'url' => 'http://www.baidu.com', 'data' => array( 'first' => array( 'value' => '你好,你已报名成功', 'color' => '#173177', ), 'keynote1' => array( 'value' => '活动主题xx', ), 'keynote2' => array( 'value' => '活动时间xx', ), 'keynote3' => array( 'value' => '活动地点xx', ), 'remark' => array( 'value' => '其他信息xx', ), ), ); $result = $this->postCurl(DWeixinApi::$templateSend, $params); if (!$result->isSuccess()) { return ResultWrapper::fail($result->getData()); } return ResultWrapper::success($result->getData()); } /** * 上传图文消息素材 * @param array $articles * @return ResultWrapper */ public function uploadNews(array $articles) { $params = array( 'articles' => $articles, ); $result = $this->postCurl(DWeixinApi::$uploadNews, $params); if (!$result->isSuccess()) { return ResultWrapper::fail($result->getData()); } return ResultWrapper::success($result->getData()); } /** * 群发消息 * @param array $messageParams * @return ResultWrapper */ public function messageSendAll($messageParams, $sendObjectType) { $apiUrl = ''; if ($sendObjectType == 'group') { $apiUrl = DWeixinApi::$sendMsgByGroup; } else if ($sendObjectType == 'openid') { $apiUrl = DWeixinApi::$sendMsgByOpenids; } else { return ResultWrapper::fail("无效\$sendObjectType参数"); } $result = $this->postCurl($apiUrl, $messageParams); if (!$result->isSuccess()) { return ResultWrapper::fail($result->getData()); } return ResultWrapper::success($result->getData()); } /** * 发送post请求 * @param string $url 接口地址 * @param array $params 接口参数 * @param boolean $isJson 是否post json数据 * @return ResultWrapper */ private function postCurl($url, $params, $isJson = true) { $url = String::jointUrl($url, array('access_token' => $this->access_token)); $objCurl = new Curl($url); if ($isJson) { $params = self::json_encode($params); } $jsonResult = $objCurl->post($params); if (!$jsonResult) { return ResultWrapper::fail('接口未返回数据'); } $result = json_decode($jsonResult, true); if ($result['errcode'] != 0) { return ResultWrapper::fail($jsonResult); } return ResultWrapper::success($result); } /** * 发送get请求 * @param string $url 接口地址 * @return ResultWrapper */ private function getCurl($url) { $url = String::jointUrl($url, array('access_token' => $this->access_token)); $objCurl = new Curl($url); $jsonResult = $objCurl->get(); if (!$jsonResult) { return ResultWrapper::fail('接口未返回数据'); } $result = json_decode($jsonResult, true); if ($result['errcode'] != 0) { return ResultWrapper::fail($jsonResult); } return ResultWrapper::success($result); } /** * * 微信json数据不能包含\uxxxx格式的字符,这里重写封装下 * @param array $params * @return array */ public function json_encode(array $params) { return urldecode(json_encode(self::urlencode($params))); } private function urlencode(array $params) { if (!is_array($params)) { return $params; } $result = array(); foreach ($params as $key => $value) { if (is_scalar($value)) { if (empty($value)) { continue; } $result[$key] = urlencode($value); } else if(is_array($value)){ if (empty($value)) { continue; } $tmp_arr = self::urlencode($value); if (empty($tmp_arr)) { continue; } $result[$key] = $tmp_arr; } else { $result[$key] = urlencode($value); } } return $result; } }