WechatLib.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. <?php
  2. namespace Heanup\Library;
  3. use Heanup\Library\WeatherAdapt\Weather;
  4. /**
  5. * Class WechatLib
  6. *
  7. * @package Heanup\Library
  8. */
  9. class WechatLib
  10. {
  11. private static $instance;
  12. /**
  13. * @var int 事件类型<br><br> 100=> 私聊消息<br>200=> 群聊消息<br>300=> 暂无<br>400=> 群成员增加<br>410=> 群成员减少<br>500=> 收到好友请求<br>600=> 二维码收款<br>700=> 收到转账<br>800=> 软件开始启动<br>900=> 新的账号登录完成<br>910=> 账号下线
  14. */
  15. public $type;
  16. /**
  17. * @var string 1级来源id(群消息事件下,1级来源为群id,2级来源为发消息的成员id,私聊事件下都一样)
  18. */
  19. public $from_wxid;
  20. /**
  21. * @var string 1级来源昵称(比如发消息的人昵称)<br>
  22. */
  23. public $from_name;
  24. /**
  25. * @var string 2级来源id(群消息事件下,1级来源为群id,2级来源为发消息的成员id,私聊事件下都一样)
  26. */
  27. public $final_from_wxid;
  28. /**
  29. * @var string 2级来源昵称
  30. */
  31. public $final_from_name;
  32. /**
  33. * @var string 当前登录的账号(机器人)标识id
  34. */
  35. public $robot_wxid;
  36. /**
  37. * @var string 消息内容
  38. */
  39. public $msg;
  40. /**
  41. * @var int 消息类型(请务必使用新版http插件)<br><br> 1 =>文本消息 <br>3 => 图片消息 <br>34 => 语音消息 <br>42 => 名片消息 <br>43 =>视频 <br>47 => 动态表情 <br> 48 =>地理位置<br>49 => 分享链接 <br>2001 => 红包<br>2002 => 小程序<br>2003 => 群邀请 <br><br>更多请参考sdk模块常量值
  42. * )
  43. */
  44. public $msg_type;
  45. /**
  46. * @var string 如果是文件消息(图片、语音、视频、动态表情),这里则是可直接访问的网络地址,非文件消息时为空
  47. */
  48. public $file_url;
  49. /**
  50. * @var int 请求时间(时间戳10位版本)
  51. */
  52. public $time;
  53. /**
  54. * @var string 机器猫接口地址
  55. */
  56. public $url = "http://127.0.0.1:8073/send";
  57. public function __construct($config = null)
  58. {
  59. if (!empty($config)) {//未配置,使用默认配置
  60. $this->url = $config['url'];
  61. }
  62. $this->parseWechat($_POST);
  63. }
  64. /**
  65. * 单例模式使用
  66. * @param string|null $url 可爱猫API接口地址
  67. * @return WechatLib
  68. */
  69. public static function getInstance($url = null)
  70. {
  71. if (!isset(self::$instance[md5($url)])) {
  72. $instance = new self(['url' => $url]);
  73. self::$instance[md5($url)] = $instance;
  74. }
  75. return self::$instance[md5($url)];
  76. }
  77. /**
  78. * 解析可爱猫回调消息
  79. * @param $data
  80. */
  81. public function parseWechat($data)
  82. {
  83. $this->type = $data['type'];
  84. $this->from_wxid = $data['from_wxid'];
  85. $this->from_name = urldecode($data['from_name']);
  86. $this->final_from_wxid = $data['final_from_wxid'];
  87. $this->final_from_name = urldecode($data['final_from_name']);
  88. $this->robot_wxid = $data['robot_wxid'];
  89. $this->msg = $data['type']<410?urldecode($data['msg']):$data['msg'];
  90. $this->msg_type = intval($data['msg_type']);
  91. $this->file_url = $data['file_url'];
  92. $this->time = $data['time'];
  93. }
  94. /**
  95. * 发送文字消息(好友或者群)
  96. *
  97. * @access public
  98. * @param string $msg 消息内容
  99. * @param string $robwxid 登录账号id,用哪个账号去发送这条消息,不传则使用当前回调的机器人ID
  100. * @param string $to_wxid 对方的id,可以是群或者好友id,不传则表示回复当前消息
  101. * @return array
  102. */
  103. public function sendTextMsg($msg, $robwxid = null, $to_wxid = null)
  104. {
  105. $data = array();
  106. $data['type'] = 100;
  107. $data['msg'] = urlencode($msg); // 发送内容
  108. $data['to_wxid'] = $to_wxid ?: $this->from_wxid; // 对方id
  109. $data['robot_wxid'] = $robwxid ?: $this->robot_wxid; // 账户id,用哪个账号去发送这条消息
  110. $response = array('data' => json_encode($data));
  111. return $this->sendRequest($response, 'post');
  112. }
  113. /**
  114. * 发送群消息并艾特某人
  115. *
  116. * @access public
  117. * @param string $robwxid 账户id,用哪个账号去发送这条消息
  118. * @param string $group_wxid 群id
  119. * @param string $at_wxid 艾特的id,群成员的id
  120. * @param string $at_name 艾特的昵称,群成员的昵称
  121. * @param string $msg 消息内容
  122. * @return array
  123. */
  124. public function sendGroupAtMsg($robwxid, $group_wxid, $at_wxid, $at_name, $msg)
  125. {
  126. $data = array();
  127. $data['type'] = 102;
  128. $data['msg'] = urlencode($msg);
  129. $data['to_wxid'] = $group_wxid;
  130. $data['at_wxid'] = $at_wxid;
  131. $data['at_name'] = $at_name;
  132. $data['robot_wxid'] = $robwxid;
  133. $response = array('data' => json_encode($data));
  134. return $this->sendRequest($response, 'post');
  135. }
  136. /**
  137. * 发送图片消息
  138. *
  139. * @access public
  140. * @param string $img_url 图片的绝对路径,可以是本地图片地址/网络图片地址
  141. * @param string $robwxid 登录账号id,用哪个账号去发送这条消息,不传则使用当前机器人ID
  142. * @param string $to_wxid 对方的id,可以是群或者好友id,不传则表示回复当前消息
  143. * @return array
  144. */
  145. public function sendImageMsg($img_url, $robwxid = null, $to_wxid = null)
  146. {
  147. $data = array();
  148. $data['type'] = 103;
  149. $data['msg'] = $img_url;
  150. $data['to_wxid'] = $to_wxid ?: $this->from_wxid;
  151. $data['robot_wxid'] = $robwxid ?: $this->robot_wxid;
  152. $response = array('data' => json_encode($data));
  153. return $this->sendRequest($response, 'post');
  154. }
  155. /**
  156. * 发送视频消息
  157. *
  158. * @access public
  159. * @param string $mp4_path 视频文件的绝对路径
  160. * @param string $robwxid 账户id,用哪个账号去发送这条消息
  161. * @param string $to_wxid 对方的id,可以是群或者好友id
  162. * @return array
  163. */
  164. public function sendVideoMsg($mp4_path, $robwxid = null, $to_wxid = null)
  165. {
  166. $data = array();
  167. $data['type'] = 104;
  168. $data['msg'] = $mp4_path;
  169. $data['to_wxid'] = $to_wxid ?: $this->from_wxid;
  170. $data['robot_wxid'] = $robwxid ?: $this->robot_wxid;
  171. $response = array('data' => json_encode($data));
  172. return $this->sendRequest($response, 'post');
  173. }
  174. /**
  175. * 发送文件消息
  176. *
  177. * @access public
  178. * @param string $file_path 文件的绝对路径
  179. * @param string $robwxid 账户id,用哪个账号去发送这条消息
  180. * @param string $to_wxid 对方的id,可以是群或者好友id
  181. * @return array
  182. */
  183. public function sendFileMsg($file_path, $robwxid = null, $to_wxid = null)
  184. {
  185. $data = array();
  186. $data['type'] = 105;
  187. $data['msg'] = $file_path; // 发送的文件的绝对路径
  188. $data['to_wxid'] = $to_wxid ?: $this->from_wxid; // 对方id(默认发送至来源的id,也可以发给其他人)
  189. $data['robot_wxid'] = $robwxid ?: $this->robot_wxid; // 账户id,用哪个账号去发送这条消息
  190. $response = array('data' => json_encode($data));
  191. return $this->sendRequest($response, 'post');
  192. }
  193. /**
  194. * 发送动态表情
  195. *
  196. * @access public
  197. * @param string $path 动态表情文件(通常是gif)的绝对路径
  198. * @param string $robwxid 账户id,用哪个账号去发送这条消息
  199. * @param string $to_wxid 对方的id,可以是群或者好友id
  200. * @return array
  201. */
  202. public function sendEmojiMsg($path, $robwxid = null, $to_wxid = null)
  203. {
  204. $data = array();
  205. $data['type'] = 106;
  206. $data['msg'] = $path; // 发送的动态表情的绝对路径
  207. $data['to_wxid'] = $to_wxid ?: $this->from_wxid; // 对方id(默认发送至来源的id,也可以发给其他人)
  208. $data['robot_wxid'] = $robwxid ?: $this->robot_wxid; // 账户id,用哪个账号去发送这条消息
  209. $response = array('data' => json_encode($data));
  210. return $this->sendRequest($response, 'post');
  211. }
  212. /**
  213. * 发送分享链接
  214. *
  215. * @access public
  216. * @param string $title 链接标题
  217. * @param string $text 链接内容
  218. * @param string $target_url 跳转链接
  219. * @param string $pic_url 图片链接
  220. * @param string $robwxid 账户id,用哪个账号去发送这条消息
  221. * @param string $to_wxid 对方的id,可以是群或者好友id
  222. * @return array
  223. */
  224. public function sendLinkMsg($title, $text, $target_url, $pic_url, $robwxid = null, $to_wxid = null)
  225. {
  226. // 封装链接结构体
  227. $link = array();
  228. $link['title'] = $title;
  229. $link['text'] = $text;
  230. $link['url'] = $target_url;
  231. $link['pic'] = $pic_url;
  232. $data = array();
  233. $data['type'] = 107;
  234. $data['msg'] = $link; // 发送的分享链接结构体
  235. $data['to_wxid'] = $to_wxid ?: $this->from_wxid;
  236. $data['robot_wxid'] = $robwxid ?: $this->robot_wxid;
  237. $response = array('data' => json_encode($data));
  238. return $this->sendRequest($response, 'post');
  239. }
  240. /**
  241. * 发送音乐分享
  242. *
  243. * @access public
  244. * @param string $name 歌曲名字
  245. * @param string $robwxid 账户id,用哪个账号去发送这条消息
  246. * @param string $to_wxid 对方的id,可以是群或者好友id
  247. * @return array
  248. */
  249. public function sendMusicMsg($name, $robwxid = null, $to_wxid = null)
  250. {
  251. $data = array();
  252. $data['type'] = 108;
  253. $data['msg'] = $name;
  254. $data['to_wxid'] = $to_wxid ?: $this->from_wxid;
  255. $data['robot_wxid'] = $robwxid ?: $this->robot_wxid;
  256. $response = array('data' => json_encode($data));
  257. return $this->sendRequest($response, 'post');
  258. }
  259. /**
  260. * 取指定登录账号的昵称
  261. *
  262. * @access public
  263. * @param string $robwxid 账户id
  264. * @return array 账号昵称
  265. */
  266. public function getRobotName($robwxid = null)
  267. {
  268. $data = array();
  269. $data['type'] = 201;
  270. $data['robot_wxid'] = $robwxid ?: $this->robot_wxid;
  271. $response = array('data' => json_encode($data));
  272. return $this->sendRequest($response, 'post');
  273. }
  274. /**
  275. * 取指定登录账号的头像
  276. *
  277. * @access public
  278. * @param string $robwxid 账户id
  279. * @return array 头像http地址
  280. */
  281. public function getRobotHeadImageUrl($robwxid = null)
  282. {
  283. $data = array();
  284. $data['type'] = 202;
  285. $data['robot_wxid'] = $robwxid ?: $this->robot_wxid; // 账户id
  286. $response = array('data' => json_encode($data));
  287. return $this->sendRequest($response, 'post');
  288. }
  289. /**
  290. * 取登录账号列表
  291. *
  292. * @access public
  293. * @return array 当前框架已登录的账号信息列表
  294. */
  295. public function getLoggedAccount_list()
  296. {
  297. $data = array();
  298. $data['type'] = 203;
  299. $response = array('data' => json_encode($data));
  300. return $this->sendRequest($response, 'post');
  301. }
  302. /**
  303. * 取好友列表
  304. *
  305. * @access public
  306. * @param string $robwxid 账户id
  307. * @param int $is_refresh 是否刷新
  308. * @return array 当前框架已登录的账号信息列表
  309. */
  310. public function getFriendList($robwxid = '', $is_refresh = 0)
  311. {
  312. $data = array();
  313. $data['type'] = 204;
  314. $data['robot_wxid'] = $robwxid; // 账户id(可选,如果填空字符串,即取所有登录账号的好友列表,反正取指定账号的列表)
  315. $data['is_refresh'] = $is_refresh; // 是否刷新列表,0 从缓存获取 / 1 刷新并获取
  316. $response = array('data' => json_encode($data));
  317. return $this->sendRequest($response, 'post');
  318. }
  319. /**
  320. * 取群聊列表
  321. *
  322. * @access public
  323. * @param string $robwxid 账户id
  324. * @param int $is_refresh 是否刷新
  325. * @return array 当前框架已登录的账号信息列表
  326. */
  327. public function getGroupList($robwxid = '', $is_refresh = 0)
  328. {
  329. $data = array();
  330. $data['type'] = 205;
  331. $data['robot_wxid'] = $robwxid; // 账户id(可选,如果填空字符串,即取所有登录账号的好友列表,反正取指定账号的列表)
  332. $data['is_refresh'] = $is_refresh; // 是否刷新列表,0 从缓存获取 / 1 刷新并获取
  333. $response = array('data' => json_encode($data));
  334. return $this->sendRequest($response, 'post');
  335. }
  336. /**
  337. * 取群成员列表
  338. *
  339. * @access public
  340. * @param string $robwxid 账户id
  341. * @param string $group_wxid 群id
  342. * @param int $is_refresh 是否刷新
  343. * @return array 当前框架已登录的账号信息列表
  344. */
  345. public function getGroupMemberList($robwxid, $group_wxid, $is_refresh = 0)
  346. {
  347. $data = array();
  348. $data['type'] = 206;
  349. $data['robot_wxid'] = $robwxid; // 账户id
  350. $data['group_wxid'] = $group_wxid; // 群id
  351. $data['is_refresh'] = $is_refresh; // 是否刷新列表,0 从缓存获取 / 1 刷新并获取
  352. $response = array('data' => json_encode($data));
  353. $result = $this->sendRequest($response, 'post');
  354. return json_decode($result, true);
  355. }
  356. /**
  357. * 取群成员资料
  358. *
  359. * @access public
  360. * @param string $robwxid 账户id
  361. * @param string $group_wxid 群id
  362. * @param string $member_wxid 群成员id
  363. * @return array
  364. */
  365. public function getGroupMember($robwxid, $group_wxid, $member_wxid)
  366. {
  367. $data = array();
  368. $data['type'] = 207;
  369. $data['robot_wxid'] = $robwxid; // 账户id,取哪个账号的资料
  370. $data['group_wxid'] = $group_wxid; // 群id
  371. $data['member_wxid'] = $member_wxid; // 群成员id
  372. $response = array('data' => json_encode($data));
  373. return $this->sendRequest($response, 'post');
  374. }
  375. /**
  376. * 接收好友转账
  377. *
  378. * @access public
  379. * @param string $robwxid 账户id
  380. * @param string $friend_wxid 朋友id
  381. * @param $json_string
  382. * @return array
  383. */
  384. public function acceptTransfer($robwxid = null, $friend_wxid = null, $json_string = null)
  385. {
  386. $data = array();
  387. $data['type'] = 301;
  388. $data['robot_wxid'] = $robwxid ?: $this->robot_wxid; // 账户id
  389. $data['friend_wxid'] = $friend_wxid ?: $this->from_wxid; // 朋友id
  390. $data['msg'] = $json_string ?: $this->msg; // 转账事件原消息
  391. $response = array('data' => json_encode($data));
  392. return $this->sendRequest($response, 'post');
  393. }
  394. /**
  395. * 同意群聊邀请
  396. *
  397. * @access public
  398. * @param string $robwxid 账户id
  399. * @param string $json_string 同步消息事件中群聊邀请原消息
  400. * @return array
  401. */
  402. public function agreeGroupInvite($robwxid = null, $json_string = null)
  403. {
  404. $data = array();
  405. $data['type'] = 302;
  406. $data['robot_wxid'] = $robwxid ?: $this->robot_wxid; // 账户id
  407. $data['msg'] = $json_string ?: $this->msg; // 同步消息事件中群聊邀请原消息
  408. $response = array('data' => json_encode($data));
  409. return $this->sendRequest($response, 'post');
  410. }
  411. /**
  412. * 同意好友请求
  413. *
  414. * @access public
  415. * @param string $robwxid 账户id
  416. * @param string $json_string 好友请求事件中原消息
  417. * @return array
  418. */
  419. public function agreeFriendVerify($robwxid = null, $json_string = null)
  420. {
  421. $data = array();
  422. $data['type'] = 303;
  423. $data['robot_wxid'] = $robwxid ?: $this->robot_wxid; // 账户id
  424. $data['msg'] = $json_string ?: $this->msg; // 好友请求事件中原消息
  425. $response = array('data' => json_encode($data));
  426. return $this->sendRequest($response, 'post');
  427. }
  428. /**
  429. * 修改好友备注
  430. *
  431. * @access public
  432. * @param string $robwxid 机器人账户id
  433. * @param string $friend_wxid 好友id
  434. * @param string $note 新备注(空字符串则是删除备注)
  435. * @return array
  436. */
  437. public function setFriendNote($robwxid, $friend_wxid, $note)
  438. {
  439. $data = array();
  440. $data['type'] = 304;
  441. $data['robot_wxid'] = $robwxid;
  442. $data['friend_wxid'] = $friend_wxid;
  443. $data['note'] = $note;
  444. $response = array('data' => json_encode($data));
  445. return $this->sendRequest($response, 'post');
  446. }
  447. /**
  448. * 删除好友
  449. *
  450. * @access public
  451. * @param string $robwxid 机器人账户id
  452. * @param string $friend_wxid 好友id
  453. * @return array
  454. */
  455. public function deleteFriend($robwxid, $friend_wxid)
  456. {
  457. $data = array();
  458. $data['type'] = 305;
  459. $data['robot_wxid'] = $robwxid;
  460. $data['friend_wxid'] = $friend_wxid;
  461. $response = array('data' => json_encode($data));
  462. return $this->sendRequest($response, 'post');
  463. }
  464. /**
  465. * 踢出群成员
  466. *
  467. * @access public
  468. * @param string $member_wxid 群成员id
  469. * @param string $group_wxid 群id
  470. * @param string $robwxid 账户id
  471. * @return array|string
  472. */
  473. public function removeGroupMember($member_wxid, $group_wxid = null, $robwxid = null)
  474. {
  475. $data = array();
  476. $data['type'] = 306;
  477. $data['robot_wxid'] = $robwxid ?: $this->robot_wxid;
  478. $data['group_wxid'] = $group_wxid ?: $this->from_wxid;
  479. $data['member_wxid'] = $member_wxid;
  480. $response = array('data' => json_encode($data));
  481. return $this->sendRequest($response, 'post');
  482. }
  483. /**
  484. * 修改群名称
  485. *
  486. * @access public
  487. * @param string $robwxid 账户id
  488. * @param string $group_wxid 群id
  489. * @param string $group_name 新群名
  490. * @return array
  491. */
  492. public function setGroupName($robwxid, $group_wxid, $group_name)
  493. {
  494. $data = array();
  495. $data['type'] = 307;
  496. $data['robot_wxid'] = $robwxid; // 账户id
  497. $data['group_wxid'] = $group_wxid; // 群id
  498. $data['group_name'] = $group_name; // 新群名
  499. $response = array('data' => json_encode($data));
  500. return $this->sendRequest($response, 'post');
  501. }
  502. /**
  503. * 修改群公告
  504. *
  505. * @access public
  506. * @param string $robwxid 账户id
  507. * @param string $group_wxid 群id
  508. * @param string $notice 新公告
  509. * @return array
  510. */
  511. public function setGroupNotice($robwxid, $group_wxid, $notice)
  512. {
  513. $data = array();
  514. $data['type'] = 308;
  515. $data['robot_wxid'] = $robwxid; // 账户id
  516. $data['group_wxid'] = $group_wxid; // 群id
  517. $data['notice'] = $notice; // 新公告
  518. $response = array('data' => json_encode($data));
  519. return $this->sendRequest($response, 'post');
  520. }
  521. /**
  522. * 建立新群
  523. *
  524. * @access public
  525. * @param string $robwxid 账户id
  526. * @param array $friends 三个人及以上的好友id数组,['wxid_1xxx', 'wxid_2xxx', 'wxid_3xxx', 'wxid_4xxx']
  527. * @return array
  528. */
  529. public function creatGroup($robwxid, array $friends)
  530. {
  531. $data = array();
  532. $data['type'] = 309;
  533. $data['robot_wxid'] = $robwxid; // 账户id
  534. $data['friends'] = $friends; // 好友id数组
  535. $response = array('data' => json_encode($data));
  536. return $this->sendRequest($response, 'post');
  537. }
  538. /**
  539. * 退出群聊
  540. *
  541. * @access public
  542. * @param string $robwxid 账户id
  543. * @param string $group_wxid 群id
  544. * @return array
  545. */
  546. public function quitGroup($robwxid, $group_wxid)
  547. {
  548. $data = array();
  549. $data['type'] = 310;
  550. $data['robot_wxid'] = $robwxid; // 账户id
  551. $data['group_wxid'] = $group_wxid; // 群id
  552. $response = array('data' => json_encode($data));
  553. return $this->sendRequest($response, 'post');
  554. }
  555. /**
  556. * 邀请加入群聊
  557. *
  558. * @access public
  559. * @param string $robwxid 账户id
  560. * @param string $group_wxid 群id
  561. * @param string $friend_wxid 好友id
  562. * @return array
  563. */
  564. public function inviteInGroup($robwxid, $group_wxid, $friend_wxid)
  565. {
  566. $data = array();
  567. $data['type'] = 311;
  568. $data['robot_wxid'] = $robwxid;
  569. $data['group_wxid'] = $group_wxid;
  570. $data['friend_wxid'] = $friend_wxid;
  571. $response = array('data' => json_encode($data));
  572. return $this->sendRequest($response, 'post');
  573. }
  574. /**
  575. * 执行一个 HTTP 请求,仅仅是post组件,其他语言请自行替换即可
  576. *
  577. * @param mixed $params 表单参数
  578. * @param int $timeout 超时时间
  579. * @param string $method 请求方法 post / get
  580. * @return array|string
  581. */
  582. public function sendRequest($params, $method = 'get', $timeout = 3)
  583. {
  584. $curl = curl_init();
  585. $is_https = stripos($this->url, 'https://') === 0 ? true : false;
  586. if ('get' == $method) {//以GET方式发送请求
  587. curl_setopt($curl, CURLOPT_URL, $this->url);
  588. } else {//以POST方式发送请求
  589. curl_setopt($curl, CURLOPT_URL, $this->url);
  590. curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
  591. curl_setopt($curl, CURLOPT_POSTFIELDS, $params);//设置传送的参数
  592. }
  593. curl_setopt($curl, CURLOPT_HEADER, false);//设置header
  594. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);//要求结果为字符串且输出到屏幕上
  595. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);//设置等待时间
  596. if ($is_https) {
  597. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
  598. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
  599. }
  600. $res = curl_exec($curl);//运行curl
  601. $err = curl_error($curl);
  602. if (false === $res || !empty($err)) {
  603. // $Errno = curl_errno($curl);
  604. // $Info = curl_getinfo($curl);
  605. // curl_close($curl);
  606. return false;//$err . ' result: ' . $res . 'error_msg: ' . $Errno;
  607. }
  608. curl_close($curl);//关闭curl
  609. $res = json_decode($res, true);
  610. $data = json_decode($res['data'], true);
  611. if ($data) {
  612. $res = $data;
  613. } else {
  614. $res = urldecode($res['data']);
  615. }
  616. return $res;
  617. }
  618. }