123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538 |
- <?php
- namespace Cas\Module;
- use KIF\Verify;
- use KIF\Data\ResultWrapper;
- use KIF\Curl;
- use KIF\String\String;
- use Cas\Dao\WeixinApi as DWeixinApi;
- /**
- *
- * 微信公众平台接口
- * @author lishumingoo@gmail.com
- */
- class WeixinApi {
- private $access_token;
-
- public function __construct() {
- $this->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;
- }
- }
|