WeixinApi.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. <?php
  2. namespace Cas\Module;
  3. use KIF\Verify;
  4. use KIF\Data\ResultWrapper;
  5. use KIF\Curl;
  6. use KIF\String\String;
  7. use Cas\Dao\WeixinApi as DWeixinApi;
  8. /**
  9. *
  10. * 微信公众平台接口
  11. * @author lishumingoo@gmail.com
  12. */
  13. class WeixinApi {
  14. private $access_token;
  15. public function __construct() {
  16. $this->access_token = WeixinBackAccount::getAccessToken();
  17. }
  18. /**
  19. *
  20. * 创建临时二维码
  21. * @param int $scene_id
  22. * @return ResultWrapper
  23. */
  24. public function createTempQrcode($scene_id) {
  25. $ticketResult = $this->createTicket('QR_SCENE', $scene_id);
  26. if (!$ticketResult->isSuccess()) {
  27. return ResultWrapper::fail($ticketResult->getData());
  28. }
  29. $result = $ticketResult->getData();
  30. $qrcode = $this->exchangeQrcode($result['ticket']);
  31. return ResultWrapper::success($qrcode);
  32. }
  33. /**
  34. *
  35. * 创建永久二维码
  36. * @param int $scene_id
  37. * @return ResultWrapper
  38. */
  39. public function createSteadyQrcode($scene_id) {
  40. $ticketResult = $this->createTicket('QR_LIMIT_SCENE', $scene_id);
  41. if (!$ticketResult->isSuccess()) {
  42. return ResultWrapper::fail($ticketResult->getData());
  43. }
  44. $result = $ticketResult->getData();
  45. $qrcode = $this->exchangeQrcode($result['ticket']);
  46. return ResultWrapper::success($qrcode);
  47. }
  48. /**
  49. *
  50. * 创建二维码ticket
  51. * @param string $action_name
  52. * @param int $scene_id
  53. * @return ResultWrapper
  54. */
  55. public function createTicket($action_name, $scene_id) {
  56. if (!$action_name) {
  57. return ResultWrapper::fail('无效 action_name 参数');
  58. }
  59. if (!Verify::unsignedInt($scene_id)) {
  60. return ResultWrapper::fail('无效 scene_id 参数');
  61. }
  62. $params = array(
  63. 'action_name' => $action_name,
  64. 'action_info' => array(
  65. 'scene' => array(
  66. 'scene_id' => $scene_id,
  67. ),
  68. ),
  69. );
  70. if ($action_name == 'QR_SCENE') {
  71. $params['expire_seconds'] = DWeixinApi::$expire_seconds;
  72. }
  73. $result = $this->postCurl(DWeixinApi::$create_ticket_api, $params);
  74. if (!$result->isSuccess()) {
  75. return ResultWrapper::fail($result->getData());
  76. }
  77. return ResultWrapper::success($result->getData());
  78. }
  79. /**
  80. *
  81. * 通过ticket换取二维码地址
  82. * @param string $ticket
  83. * @return boolean|string
  84. */
  85. public function exchangeQrcode($ticket) {
  86. if (!$ticket) {
  87. return false;
  88. }
  89. $args = array(
  90. 'ticket' => urlencode($ticket),
  91. );
  92. return DWeixinApi::$exchange_qrcode_api . '?ticket=' . urlencode($ticket);
  93. }
  94. /**
  95. *
  96. * 查询所有分组
  97. * @return ResultWrapper
  98. */
  99. public function searchAllGroups() {
  100. $result = $this->getCurl(DWeixinApi::$search_all_groups);
  101. if (!$result->isSuccess()) {
  102. return ResultWrapper::fail($result->getData());
  103. }
  104. return ResultWrapper::success($result->getData());
  105. }
  106. /**
  107. *
  108. * 创建分组
  109. * @param string $groupName
  110. * @return ResultWrapper
  111. */
  112. public function createGroup($groupName) {
  113. if (!$groupName) {
  114. return ResultWrapper::fail('无效组名');
  115. }
  116. $params = array(
  117. 'group' => array('name' => $groupName),
  118. );
  119. $result = $this->postCurl(DWeixinApi::$create_group, $params);
  120. if (!$result->isSuccess()) {
  121. return ResultWrapper::fail($result->getData());
  122. }
  123. return ResultWrapper::success($result->getData());
  124. }
  125. /**
  126. *
  127. * 查询用户所在分组
  128. * @param string $openid
  129. * @return ResultWrapper
  130. */
  131. public function searchMemberGroupid($openid) {
  132. if (!$openid) {
  133. return ResultWrapper::fail('无效openid');
  134. }
  135. $params = array(
  136. 'openid' => $openid,
  137. );
  138. $result = $this->postCurl(DWeixinApi::$search_member_groupid, $params);
  139. if (!$result->isSuccess()) {
  140. return ResultWrapper::fail($result->getData());
  141. }
  142. return ResultWrapper::success($result->getData());
  143. }
  144. /**
  145. *
  146. * 修改分组名
  147. * @param int $groupid
  148. * @param string $groupName
  149. * @return ResultWrapper
  150. */
  151. public function updateGroupName($groupid, $groupName) {
  152. if (!Verify::unsignedInt($groupid)) {
  153. return ResultWrapper::fail('无效分组id');
  154. }
  155. if (!$groupName) {
  156. return ResultWrapper::fail('无效组名');
  157. }
  158. $params = array(
  159. 'group' => array(
  160. 'id' => $groupid,
  161. 'name' => $groupName,
  162. ),
  163. );
  164. $result = $this->postCurl(DWeixinApi::$update_group_name, $params);
  165. if (!$result->isSuccess()) {
  166. return ResultWrapper::fail($result->getData());
  167. }
  168. return ResultWrapper::success($result->getData());
  169. }
  170. /**
  171. *
  172. * 更新用户分组
  173. * @param string $openid
  174. * @param int $to_groupid
  175. * @return ResultWrapper
  176. */
  177. public function updateMemberGroup($openid, $to_groupid) {
  178. if (!$openid) {
  179. return ResultWrapper::fail('无效openid');
  180. }
  181. if (!Verify::unsignedInt($to_groupid)) {
  182. return ResultWrapper::fail('无效分组id');
  183. }
  184. $params = array(
  185. 'openid' => $openid,
  186. 'to_groupid' => $to_groupid,
  187. );
  188. $result = $this->postCurl(DWeixinApi::$update_member_group, $params);
  189. if (!$result->isSuccess()) {
  190. return ResultWrapper::fail($result->getData());
  191. }
  192. return ResultWrapper::success($result->getData());
  193. }
  194. /**
  195. *
  196. * 创建菜单
  197. * @param array $params
  198. * @return ResultWrapper
  199. */
  200. public function createMenu($params) {
  201. $result = $this->postCurl(DWeixinApi::$create_menu, $params);
  202. if (!$result->isSuccess()) {
  203. return ResultWrapper::fail($result->getData());
  204. }
  205. return ResultWrapper::success($result->getData());
  206. }
  207. /**
  208. *
  209. * 删除菜单
  210. */
  211. public function deleteMenu() {
  212. $result = $this->getCurl(DWeixinApi::$delete_menu);
  213. if (!$result->isSuccess()) {
  214. return ResultWrapper::fail($result->getData());
  215. }
  216. return ResultWrapper::success($result->getData());
  217. }
  218. /**
  219. * 获取用户基本信息
  220. * @param string $openid
  221. * @return ResultWrapper
  222. */
  223. public function getUserInfo($openid) {
  224. if (!$openid) {
  225. return ResultWrapper::fail('无效openid');
  226. }
  227. $params = array(
  228. 'openid' => $openid,
  229. 'lang' => 'zh_CN',
  230. );
  231. $url = String::jointUrl(DWeixinApi::$get_user_info, $params);
  232. $result = $this->getCurl($url);
  233. if (!$result->isSuccess()) {
  234. return ResultWrapper::fail($result->getData());
  235. }
  236. return ResultWrapper::success($result->getData());
  237. }
  238. /**
  239. * 获取关注者列表
  240. * @param string $next_openid
  241. * @return ResultWrapper
  242. */
  243. public function getsOpenids($next_openid = null) {
  244. $params = array();
  245. if (!is_null($next_openid) && $next_openid) {
  246. $params['next_openid'] = $next_openid;
  247. }
  248. $url = String::jointUrl(DWeixinApi::$gets_openids_list, $params);
  249. $result = $this->getCurl($url);
  250. if (!$result->isSuccess()) {
  251. return ResultWrapper::fail($result->getData());
  252. }
  253. return ResultWrapper::success($result->getData());
  254. }
  255. /**
  256. * 上传多媒体文件
  257. * @param string $filePath
  258. * @param string $fileType
  259. * @return ResultWrapper
  260. */
  261. public function mediaUpload($filePath, $fileType) {
  262. if (!$filePath) {
  263. return ResultWrapper::fail('无效文件路径');
  264. }
  265. if (!in_array($fileType, array('image', 'voice', 'video', 'thumb'))) {
  266. return ResultWrapper::fail("无效文件类型:{$fileType}");
  267. }
  268. $params = array(
  269. 'media' => "@{$filePath}",
  270. );
  271. $url = String::jointUrl(DWeixinApi::$mediaUpload, array('access_token' => $this->access_token, 'type' => $fileType));
  272. $ch = curl_init($url);
  273. curl_setopt($ch, CURLOPT_POST, 1);
  274. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  275. curl_setopt($ch, CURLOPT_POST, 1);
  276. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  277. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  278. $jsonResult = curl_exec($ch);
  279. curl_close($ch);
  280. if (!$jsonResult) {
  281. return ResultWrapper::fail('接口未返回数据');
  282. }
  283. $result = json_decode($jsonResult, true);
  284. if ($result['errcode'] != 0) {
  285. return ResultWrapper::fail($jsonResult);
  286. }
  287. return ResultWrapper::success($result);
  288. }
  289. /**
  290. * 发送客服消息之:文本消息
  291. * @param string @openid
  292. * @param string $content
  293. */
  294. public function sendTextMessage($openid, $content) {
  295. if (!$openid) {
  296. return ResultWrapper::fail('无效openid');
  297. }
  298. if (!$content) {
  299. return ResultWrapper::fail('消息内容不能为空');
  300. }
  301. $params = array(
  302. 'touser' => $openid,
  303. 'msgtype' => 'text',
  304. 'text' => array(
  305. 'content' => $content,
  306. ),
  307. );
  308. $result = $this->postCurl(DWeixinApi::$sendMessage, $params);
  309. if (!$result->isSuccess()) {
  310. return ResultWrapper::fail($result->getData());
  311. }
  312. return ResultWrapper::success($result->getData());
  313. }
  314. /**
  315. * 发送模板消息
  316. * @param unknown $openid
  317. */
  318. public function sendTemplateMessage($openid) {
  319. if (!$openid) {
  320. return ResultWrapper::fail('无效openid');
  321. }
  322. $params = array(
  323. 'touser' => $openid,
  324. 'template_id' => 'DhQvR71fIoV91k0XdboZlT2lbue8GpNKUaz8xT8A1Jk',
  325. 'url' => 'http://www.baidu.com',
  326. 'data' => array(
  327. 'first' => array(
  328. 'value' => '你好,你已报名成功',
  329. 'color' => '#173177',
  330. ),
  331. 'keynote1' => array(
  332. 'value' => '活动主题xx',
  333. ),
  334. 'keynote2' => array(
  335. 'value' => '活动时间xx',
  336. ),
  337. 'keynote3' => array(
  338. 'value' => '活动地点xx',
  339. ),
  340. 'remark' => array(
  341. 'value' => '其他信息xx',
  342. ),
  343. ),
  344. );
  345. $result = $this->postCurl(DWeixinApi::$templateSend, $params);
  346. if (!$result->isSuccess()) {
  347. return ResultWrapper::fail($result->getData());
  348. }
  349. return ResultWrapper::success($result->getData());
  350. }
  351. /**
  352. * 上传图文消息素材
  353. * @param array $articles
  354. * @return ResultWrapper
  355. */
  356. public function uploadNews(array $articles) {
  357. $params = array(
  358. 'articles' => $articles,
  359. );
  360. $result = $this->postCurl(DWeixinApi::$uploadNews, $params);
  361. if (!$result->isSuccess()) {
  362. return ResultWrapper::fail($result->getData());
  363. }
  364. return ResultWrapper::success($result->getData());
  365. }
  366. /**
  367. * 群发消息
  368. * @param array $messageParams
  369. * @return ResultWrapper
  370. */
  371. public function messageSendAll($messageParams, $sendObjectType) {
  372. $apiUrl = '';
  373. if ($sendObjectType == 'group') {
  374. $apiUrl = DWeixinApi::$sendMsgByGroup;
  375. } else if ($sendObjectType == 'openid') {
  376. $apiUrl = DWeixinApi::$sendMsgByOpenids;
  377. } else {
  378. return ResultWrapper::fail("无效\$sendObjectType参数");
  379. }
  380. $result = $this->postCurl($apiUrl, $messageParams);
  381. if (!$result->isSuccess()) {
  382. return ResultWrapper::fail($result->getData());
  383. }
  384. return ResultWrapper::success($result->getData());
  385. }
  386. /**
  387. * 发送post请求
  388. * @param string $url 接口地址
  389. * @param array $params 接口参数
  390. * @param boolean $isJson 是否post json数据
  391. * @return ResultWrapper
  392. */
  393. private function postCurl($url, $params, $isJson = true) {
  394. $url = String::jointUrl($url, array('access_token' => $this->access_token));
  395. $objCurl = new Curl($url);
  396. if ($isJson) {
  397. $params = self::json_encode($params);
  398. }
  399. $jsonResult = $objCurl->post($params);
  400. if (!$jsonResult) {
  401. return ResultWrapper::fail('接口未返回数据');
  402. }
  403. $result = json_decode($jsonResult, true);
  404. if ($result['errcode'] != 0) {
  405. return ResultWrapper::fail($jsonResult);
  406. }
  407. return ResultWrapper::success($result);
  408. }
  409. /**
  410. * 发送get请求
  411. * @param string $url 接口地址
  412. * @return ResultWrapper
  413. */
  414. private function getCurl($url) {
  415. $url = String::jointUrl($url, array('access_token' => $this->access_token));
  416. $objCurl = new Curl($url);
  417. $jsonResult = $objCurl->get();
  418. if (!$jsonResult) {
  419. return ResultWrapper::fail('接口未返回数据');
  420. }
  421. $result = json_decode($jsonResult, true);
  422. if ($result['errcode'] != 0) {
  423. return ResultWrapper::fail($jsonResult);
  424. }
  425. return ResultWrapper::success($result);
  426. }
  427. /**
  428. *
  429. * 微信json数据不能包含\uxxxx格式的字符,这里重写封装下
  430. * @param array $params
  431. * @return array
  432. */
  433. public function json_encode(array $params) {
  434. return urldecode(json_encode(self::urlencode($params)));
  435. }
  436. private function urlencode(array $params) {
  437. if (!is_array($params)) {
  438. return $params;
  439. }
  440. $result = array();
  441. foreach ($params as $key => $value) {
  442. if (is_scalar($value)) {
  443. if (empty($value)) {
  444. continue;
  445. }
  446. $result[$key] = urlencode($value);
  447. } else if(is_array($value)){
  448. if (empty($value)) {
  449. continue;
  450. }
  451. $tmp_arr = self::urlencode($value);
  452. if (empty($tmp_arr)) {
  453. continue;
  454. }
  455. $result[$key] = $tmp_arr;
  456. } else {
  457. $result[$key] = urlencode($value);
  458. }
  459. }
  460. return $result;
  461. }
  462. }