You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

824 lines
22 KiB

7 years ago
  1. <?php
  2. namespace App\Common;
  3. //环信即时云通信
  4. class Easemob
  5. {
  6. //const APP_KEY = 'b82cd9fcd0cbb92866d6d726';
  7. //const APP_SECRET = 'ac92d336f90842051dc12f49';
  8. private $client_id = 'YXA6fLiQwHNeEee1cGVk2WW3qw';
  9. private $client_secret = 'YXA6Re59-iEqGB2aohfoaeeAgEtRYiw';
  10. private $org_name = '1194170724115734';
  11. private $app_name = 'cuobian';
  12. private $url;
  13. /**
  14. * 初始化参数
  15. *
  16. * @param array $options
  17. * @param $options['client_id']
  18. * @param $options['client_secret']
  19. * @param $options['org_name']
  20. * @param $options['app_name']
  21. */
  22. public function __construct($options='')
  23. {
  24. /* $this->client_id = isset ( $options ['client_id'] ) ? $options ['client_id'] : '';
  25. $this->client_secret = isset ( $options ['client_secret'] ) ? $options ['client_secret'] : '';
  26. $this->org_name = isset ( $options ['org_name'] ) ? $options ['org_name'] : '';
  27. $this->app_name = isset ( $options ['app_name'] ) ? $options ['app_name'] : ''; */
  28. if (! empty ( $this->org_name ) && ! empty ( $this->app_name ))
  29. {
  30. $this->url = 'https://a1.easemob.com/' . $this->org_name . '/' . $this->app_name . '/';
  31. }
  32. }
  33. /**
  34. * 获取app管理员token
  35. * Path: /{org_name}/{app_name}/token
  36. * HTTP Method: POST
  37. * URL Params:
  38. * Request Headers: {“Content-Type”:”application/json”}
  39. * Request Body: {“grant_type”: “client_credentials”,”client_id”: {APP的client_id},”client_secret”: {APP的client_secret}}
  40. */
  41. public function getToken()
  42. {
  43. $url = $this->url . "token";
  44. $body=array(
  45. "grant_type"=> "client_credentials",
  46. "client_id"=> $this->client_id,
  47. "client_secret"=> $this->client_secret
  48. );
  49. $res = $this->postCurl($url,$body);
  50. $tokenResult = array();
  51. $tokenResult = json_decode($res, true);
  52. //var_dump($tokenResult);
  53. //return "Authorization:Bearer ". $tokenResult["access_token"];
  54. return $tokenResult["access_token"];
  55. }
  56. /**
  57. * 开放注册模式
  58. *
  59. * @param $options['username'] 用户名
  60. * @param $options['password'] 密码
  61. */
  62. public function openRegister($options)
  63. {
  64. $url = $this->url . "users";
  65. $result = $this->postCurl ( $url, $options, $head = 0 );
  66. return $result;
  67. }
  68. /**
  69. * 授权注册模式 || 批量注册
  70. *
  71. * @param $options['username'] 用户名
  72. * @param $options['password'] 密码
  73. * 批量注册传二维数组
  74. */
  75. public function imRegister($options)
  76. {
  77. $url = $this->url . "users";
  78. $access_token = $this->getToken();
  79. $header [] = 'Authorization: Bearer ' . $access_token;
  80. $result = $this->postCurl ( $url, $options, $header );
  81. return $result;
  82. }
  83. /**
  84. * 获取指定用户详情
  85. *
  86. * @param $username 用户名
  87. */
  88. public function userDetails($username)
  89. {
  90. $url = $this->url . "users/" . $username;
  91. $access_token = $this->getToken();
  92. $header [] = 'Authorization: Bearer ' . $access_token;
  93. $result = $this->postCurl ( $url, '', $header, $type = 'GET' );
  94. return $result;
  95. }
  96. /**
  97. * 重置用户密码
  98. *
  99. * @param $options['username'] 用户名
  100. * @param $options['password'] 密码
  101. * @param $options['newpassword'] 新密码
  102. */
  103. public function editPassword($options)
  104. {
  105. $url = $this->url . "users/" . $options['username'] . "/password";
  106. $access_token = $this->getToken();
  107. $header [] = 'Authorization: Bearer ' . $access_token;
  108. $result = $this->postCurl ( $url, $options, $header, $type = 'PUT');
  109. return $result;
  110. }
  111. /**
  112. * 删除用户
  113. *
  114. * @param $username 用户名
  115. */
  116. public function deleteUser($username)
  117. {
  118. $url = $this->url . "users/" . $username;
  119. $access_token = $this->getToken();
  120. $header [] = 'Authorization: Bearer ' . $access_token;
  121. $result = $this->postCurl ( $url, '', $header, $type = 'DELETE' );
  122. }
  123. /**
  124. * 批量删除用户
  125. * 描述:删除某个app下指定数量的环信账号。上述url可一次删除300个用户,数值可以修改 建议这个数值在100-500之间,不要过大
  126. *
  127. * @param $limit="300" 默认为300条
  128. * @param $ql 删除条件
  129. * 如ql=order+by+created+desc 按照创建时间来排序(降序)
  130. */
  131. public function batchDeleteUser($limit = "300", $ql = '')
  132. {
  133. $url = $this->url . "users?limit=" . $limit;
  134. if (! empty ( $ql ))
  135. {
  136. $url = $this->url . "users?ql=" . $ql . "&limit=" . $limit;
  137. }
  138. $access_token = $this->getToken();
  139. $header [] = 'Authorization: Bearer ' . $access_token;
  140. $result = $this->postCurl ( $url, '', $header, $type = 'DELETE' );
  141. }
  142. /**
  143. * 给一个用户添加一个好友
  144. *
  145. * @param
  146. * $owner_username
  147. * @param
  148. * $friend_username
  149. */
  150. public function addFriend($owner_username, $friend_username)
  151. {
  152. $url = $this->url . "users/" . $owner_username . "/contacts/users/" . $friend_username;
  153. $access_token = $this->getToken();
  154. $header [] = 'Authorization: Bearer ' . $access_token;
  155. $result = $this->postCurl ( $url, '', $header );
  156. }
  157. /**
  158. * 删除好友
  159. *
  160. * @param
  161. * $owner_username
  162. * @param
  163. * $friend_username
  164. */
  165. public function deleteFriend($owner_username, $friend_username)
  166. {
  167. $url = $this->url . "users/" . $owner_username . "/contacts/users/" . $friend_username;
  168. $access_token = $this->getToken();
  169. $header [] = 'Authorization: Bearer ' . $access_token;
  170. $result = $this->postCurl ( $url, '', $header, $type = "DELETE" );
  171. }
  172. /**
  173. * 查看用户的好友
  174. *
  175. * @param
  176. * $owner_username
  177. */
  178. public function showFriend($owner_username)
  179. {
  180. $url = $this->url . "users/" . $owner_username . "/contacts/users/";
  181. $access_token = $this->getToken();
  182. $header [] = 'Authorization: Bearer ' . $access_token;
  183. $result = $this->postCurl ( $url, '', $header, $type = "GET" );
  184. }
  185. // +----------------------------------------------------------------------
  186. // | 聊天相关的方法
  187. // +----------------------------------------------------------------------
  188. /**
  189. * 查看用户是否在线
  190. *
  191. * @param
  192. * $username
  193. */
  194. public function isOnline($username)
  195. {
  196. $url = $this->url . "users/" . $username . "/status";
  197. $access_token = $this->getToken();
  198. $header [] = 'Authorization: Bearer ' . $access_token;
  199. $result = $this->postCurl ( $url, '', $header, $type = "GET" );
  200. return $result;
  201. }
  202. /**
  203. * 发送消息
  204. *
  205. * @param string $from_user
  206. * 发送方用户名
  207. * @param array $usernames
  208. * array('1','2')
  209. * @param string $target_type
  210. * 默认为:users 描述:给一个或者多个用户(users)或者群组发送消息(chatgroups)
  211. * @param string $msg_content
  212. * @param array $ext
  213. * 自定义参数
  214. */
  215. public function sendMessage($from_user = "admin", $usernames, $msg_type='txt', $msg_content='', $target_type = "users", $ext=[])
  216. {
  217. $option ['target_type'] = $target_type;
  218. $option ['target'] = $usernames;
  219. $params ['type'] = $msg_type;
  220. if($msg_type == 'txt')
  221. {
  222. $params ['msg'] = $msg_content;
  223. }
  224. elseif($msg_type == 'cmd')
  225. {
  226. $params ['action'] = $msg_content;
  227. }
  228. $option ['msg'] = $params;
  229. $option ['from'] = $from_user;
  230. if($ext)
  231. {
  232. $option ['ext'] = $ext;
  233. }
  234. $url = $this->url . "messages";
  235. $access_token = $this->getToken();
  236. $header [] = 'Authorization: Bearer ' . $access_token;
  237. $result = $this->postCurl ( $url, $option, $header );
  238. return $result;
  239. }
  240. //--------------------------------------------------------上传下载
  241. /**
  242. * 上传图片或文件
  243. */
  244. public function uploadFile($filePath)
  245. {
  246. $url=$this->url.'chatfiles';
  247. $file=file_get_contents($filePath);
  248. $body['file']=$file;
  249. $header=array('Content-type: multipart/form-data','Authorization: Bearer ' . $this->getToken(),"restrict-access:true");
  250. $result=$this->postCurl($url,$body,$header,'XXX');
  251. return $result;
  252. }
  253. /**
  254. * 下载文件或图片
  255. */
  256. public function downloadFile($uuid,$shareSecret,$ext)
  257. {
  258. $url = $this->url . 'chatfiles/' . $uuid;
  259. $header = array("share-secret:" . $shareSecret, "Accept:application/octet-stream", 'Authorization: Bearer ' . $this->getToken(),);
  260. if ($ext=="png")
  261. {
  262. $result = $this->postCurl($url,'',$header,'GET');
  263. }
  264. else
  265. {
  266. $result = $this->getFile($url);
  267. }
  268. $filename = md5(time().mt_rand(10, 99)).".".$ext; //新图片名称
  269. if(!file_exists("resource/down"))
  270. {
  271. mkdir("resource/down/");
  272. }
  273. $file = @fopen("resource/down/".$filename,"w+");//打开文件准备写入
  274. @fwrite($file,$result);//写入
  275. fclose($file);//关闭
  276. return $filename;
  277. }
  278. public function getFile($url)
  279. {
  280. set_time_limit(0); // unlimited max execution time
  281. $ch = curl_init($url);
  282. curl_setopt($ch, CURLOPT_TIMEOUT, 600); //max 10 minutes
  283. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  284. curl_setopt($ch, CURLOPT_HEADER, false);
  285. $result = curl_exec($ch);
  286. curl_close($ch);
  287. return $result;
  288. }
  289. /**
  290. * 下载图片缩略图
  291. */
  292. public function downloadThumbnail($uuid,$shareSecret)
  293. {
  294. $url=$this->url.'chatfiles/'.$uuid;
  295. $header = array("share-secret:".$shareSecret,"Accept:application/octet-stream",'Authorization: Bearer ' . $this->getToken(),"thumbnail:true");
  296. $result=$this->postCurl($url,'',$header,'GET');
  297. $filename = md5(time().mt_rand(10, 99))."th.png"; //新图片名称
  298. if(!file_exists("resource/down"))
  299. {
  300. //mkdir("../image/down");
  301. mkdirs("resource/down/");
  302. }
  303. $file = @fopen("resource/down/".$filename,"w+");//打开文件准备写入
  304. @fwrite($file,$result);//写入
  305. fclose($file);//关闭
  306. return $filename;
  307. }
  308. //--------------------------------------------------------发送消息
  309. /**
  310. * 发送文本消息
  311. */
  312. public function sendText($from="admin",$target_type,$target,$content,$ext)
  313. {
  314. $url=$this->url.'messages';
  315. $body['target_type']=$target_type;
  316. $body['target']=$target;
  317. $options['type']="txt";
  318. $options['msg']=$content;
  319. $body['msg']=$options;
  320. $body['from']=$from;
  321. $body['ext']=$ext;
  322. $access_token = $this->getToken();
  323. $header [] = 'Authorization: Bearer ' . $access_token;
  324. $result=$this->postCurl($url,$body,$header);
  325. return $result;
  326. }
  327. /**
  328. * 发送透传消息
  329. */
  330. public function sendCmd($from="admin",$target_type,$target,$action,$ext)
  331. {
  332. $url=$this->url.'messages';
  333. $body['target_type']=$target_type;
  334. $body['target']=$target;
  335. $options['type']="cmd";
  336. $options['action']=$action;
  337. $body['msg']=$options;
  338. $body['from']=$from;
  339. $body['ext']=$ext;
  340. $access_token = $this->getToken();
  341. $header [] = 'Authorization: Bearer ' . $access_token;
  342. $result=$this->postCurl($url,$body,$header);
  343. return $result;
  344. }
  345. /**
  346. * 发图片消息
  347. */
  348. public function sendImage($filePath,$from="admin",$target_type,$target,$filename,$ext)
  349. {
  350. $result=$this->uploadFile($filePath);
  351. $uri=$result['uri'];
  352. $uuid=$result['entities'][0]['uuid'];
  353. $shareSecret=$result['entities'][0]['share-secret'];
  354. $url=$this->url.'messages';
  355. $body['target_type']=$target_type;
  356. $body['target']=$target;
  357. $options['type']="img";
  358. $options['url']=$uri.'/'.$uuid;
  359. $options['filename']=$filename;
  360. $options['secret']=$shareSecret;
  361. $options['size']=array(
  362. "width"=>480,
  363. "height"=>720
  364. );
  365. $body['msg']=$options;
  366. $body['from']=$from;
  367. $body['ext']=$ext;
  368. $access_token = $this->getToken();
  369. $header [] = 'Authorization: Bearer ' . $access_token;
  370. $result=$this->postCurl($url,$body,$header);
  371. return $result;
  372. }
  373. /**
  374. * 发语音消息
  375. */
  376. public function sendAudio($filePath,$from="admin",$target_type,$target,$filename,$length,$ext)
  377. {
  378. $result=$this->uploadFile($filePath);
  379. $uri=$result['uri'];
  380. $uuid=$result['entities'][0]['uuid'];
  381. $shareSecret=$result['entities'][0]['share-secret'];
  382. $url=$this->url.'messages';
  383. $body['target_type']=$target_type;
  384. $body['target']=$target;
  385. $options['type']="audio";
  386. $options['url']=$uri.'/'.$uuid;
  387. $options['filename']=$filename;
  388. $options['length']=$length;
  389. $options['secret']=$shareSecret;
  390. $body['msg']=$options;
  391. $body['from']=$from;
  392. $body['ext']=$ext;
  393. $access_token = $this->getToken();
  394. $header [] = 'Authorization: Bearer ' . $access_token;
  395. $result=$this->postCurl($url,$body,$header);
  396. return $result;
  397. }
  398. /**
  399. * 发视频消息
  400. */
  401. public function sendVedio($filePath,$from="admin",$target_type,$target,$filename,$length,$thumb,$thumb_secret,$ext)
  402. {
  403. $result=$this->uploadFile($filePath);
  404. $uri=$result['uri'];
  405. $uuid=$result['entities'][0]['uuid'];
  406. $shareSecret=$result['entities'][0]['share-secret'];
  407. $url=$this->url.'messages';
  408. $body['target_type']=$target_type;
  409. $body['target']=$target;
  410. $options['type']="video";
  411. $options['url']=$uri.'/'.$uuid;
  412. $options['filename']=$filename;
  413. $options['thumb']=$thumb;
  414. $options['length']=$length;
  415. $options['secret']=$shareSecret;
  416. $options['thumb_secret']=$thumb_secret;
  417. $body['msg']=$options;
  418. $body['from']=$from;
  419. $body['ext']=$ext;
  420. $access_token = $this->getToken();
  421. $header [] = 'Authorization: Bearer ' . $access_token;
  422. $result=$this->postCurl($url,$body,$header);
  423. return $result;
  424. }
  425. /**
  426. * 发文件消息
  427. */
  428. public function sendFile($filePath,$from="admin",$target_type,$target,$filename,$length,$ext)
  429. {
  430. $result=$this->uploadFile($filePath);
  431. $uri=$result['uri'];
  432. $uuid=$result['entities'][0]['uuid'];
  433. $shareSecret=$result['entities'][0]['share-secret'];
  434. $url=$GLOBALS['base_url'].'messages';
  435. $body['target_type']=$target_type;
  436. $body['target']=$target;
  437. $options['type']="file";
  438. $options['url']=$uri.'/'.$uuid;
  439. $options['filename']=$filename;
  440. $options['length']=$length;
  441. $options['secret']=$shareSecret;
  442. $body['msg']=$options;
  443. $body['from']=$from;
  444. $body['ext']=$ext;
  445. $access_token = $this->getToken();
  446. $header [] = 'Authorization: Bearer ' . $access_token;
  447. $result=postCurl($url,$body,$header);
  448. return $result;
  449. }
  450. /**
  451. * 获取app中所有的群组
  452. */
  453. public function chatGroups()
  454. {
  455. $url = $this->url . "chatgroups";
  456. $access_token = $this->getToken();
  457. $header [] = 'Authorization: Bearer ' . $access_token;
  458. $result = $this->postCurl ( $url, '', $header, $type = "GET" );
  459. return $result;
  460. }
  461. /**
  462. * 创建群组
  463. *
  464. * @param $option['groupname'] //群组名称,
  465. * 此属性为必须的
  466. * @param $option['desc'] //群组描述,
  467. * 此属性为必须的
  468. * @param $option['public'] //是否是公开群,
  469. * 此属性为必须的 true or false
  470. * @param $option['approval'] //加入公开群是否需要批准,
  471. * 没有这个属性的话默认是true, 此属性为可选的
  472. * @param $option['owner'] //群组的管理员,
  473. * 此属性为必须的
  474. * @param $option['members'] //群组成员,此属性为可选的
  475. */
  476. public function createGroups($option)
  477. {
  478. $url = $this->url . "chatgroups";
  479. $access_token = $this->getToken();
  480. $header [] = 'Authorization: Bearer ' . $access_token;
  481. $result = $this->postCurl ( $url, $option, $header );
  482. return $result;
  483. }
  484. /**
  485. * 修改群组
  486. *
  487. * @param $option['groupname'] //群组名称
  488. * @param $option['description'] //群组描述
  489. * @param $option['maxusers'] //群组成员最大数
  490. */
  491. public function updateGroups($group_id,$option)
  492. {
  493. $url = $this->url . "chatgroups/". $group_id;
  494. $access_token = $this->getToken();
  495. $header [] = 'Authorization: Bearer ' . $access_token;
  496. $result = $this->postCurl ( $url, $option, $header, $type = 'PUT' );
  497. return $result;
  498. }
  499. /**
  500. * 获取群组详情
  501. *
  502. * @param
  503. * $group_id
  504. */
  505. public function chatGroupsDetails($group_id)
  506. {
  507. $url = $this->url . "chatgroups/" . $group_id;
  508. $access_token = $this->getToken();
  509. $header [] = 'Authorization: Bearer ' . $access_token;
  510. $result = $this->postCurl ( $url, '', $header, $type = "GET" );
  511. return $result;
  512. }
  513. /**
  514. * 删除群组
  515. *
  516. * @param
  517. * $group_id
  518. */
  519. public function deleteGroups($group_id)
  520. {
  521. $url = $this->url . "chatgroups/" . $group_id;
  522. $access_token = $this->getToken();
  523. $header [] = 'Authorization: Bearer ' . $access_token;
  524. $result = $this->postCurl ( $url, '', $header, $type = "DELETE" );
  525. return $result;
  526. }
  527. /**
  528. * 获取群组成员
  529. *
  530. * @param
  531. * $group_id
  532. */
  533. public function groupsUser($group_id)
  534. {
  535. $url = $this->url . "chatgroups/" . $group_id . "/users";
  536. $access_token = $this->getToken();
  537. $header [] = 'Authorization: Bearer ' . $access_token;
  538. $result = $this->postCurl ( $url, '', $header, $type = "GET" );
  539. return $result;
  540. }
  541. /**
  542. * 群组添加成员
  543. *
  544. * @param
  545. * $group_id
  546. * @param
  547. * $username
  548. */
  549. public function addGroupsUser($group_id, $username)
  550. {
  551. $url = $this->url . "chatgroups/" . $group_id . "/users/" . $username;
  552. $access_token = $this->getToken();
  553. $header [] = 'Authorization: Bearer ' . $access_token;
  554. $result = $this->postCurl ( $url, '', $header, $type = "POST" );
  555. return $result;
  556. }
  557. /**
  558. * 群组批量添加成员
  559. *
  560. * @param
  561. * $group_id
  562. * @param
  563. * $username
  564. */
  565. public function addGroupsUsers($group_id, $usernames)
  566. {
  567. $usernames = ['usernames' => $usernames];
  568. $url = $this->url . "chatgroups/" . $group_id . "/users";
  569. $access_token = $this->getToken();
  570. $header [] = 'Authorization: Bearer ' . $access_token;
  571. $result = $this->postCurl ( $url, $usernames, $header, $type = "POST" );
  572. return $result;
  573. }
  574. /**
  575. * 群组删除成员
  576. *
  577. * @param
  578. * $group_id
  579. * @param
  580. * $username
  581. */
  582. public function delGroupsUser($group_id, $username)
  583. {
  584. $url = $this->url . "chatgroups/" . $group_id . "/users/" . $username;
  585. $access_token = $this->getToken();
  586. $header [] = 'Authorization: Bearer ' . $access_token;
  587. $result = $this->postCurl ( $url, '', $header, $type = "DELETE" );
  588. return $result;
  589. }
  590. /**
  591. * 聊天消息记录
  592. *
  593. * @param $ql 查询条件如:$ql
  594. * = "select+*+where+from='" . $uid . "'+or+to='". $uid ."'+order+by+timestamp+desc&limit=" . $limit . $cursor;
  595. * 默认为order by timestamp desc
  596. * @param $cursor 分页参数
  597. * 默认为空
  598. * @param $limit 条数
  599. * 默认20
  600. */
  601. public function chatRecord($ql = '', $cursor = '', $limit = 20)
  602. {
  603. $ql = ! empty ( $ql ) ? "ql=" . $ql : "order+by+timestamp+desc";
  604. $cursor = ! empty ( $cursor ) ? "&cursor=" . $cursor : '';
  605. $url = $this->url . "chatmessages?" . $ql . "&limit=" . $limit . $cursor;
  606. $access_token = $this->getToken();
  607. $header [] = 'Authorization: Bearer ' . $access_token;
  608. $result = $this->postCurl ( $url, '', $header, $type = "GET " );
  609. return $result;
  610. }
  611. /**
  612. * 获取Token
  613. */
  614. /* public function getToken()
  615. {
  616. $option ['grant_type'] = "client_credentials";
  617. $option ['client_id'] = $this->client_id;
  618. $option ['client_secret'] = $this->client_secret;
  619. $url = $this->url . "token";
  620. $fp = @fopen ( "easemob.txt", 'r' );
  621. if ($fp)
  622. {
  623. $arr = unserialize ( fgets ( $fp ) );
  624. if ($arr ['expires_in'] < time ())
  625. {
  626. $result = $this->postCurl ( $url, $option, $head = 0 );
  627. $result = json_decode($result);
  628. $result ['expires_in'] = $result ['expires_in'] + time ();
  629. @fwrite ( $fp, serialize ( $result ) );
  630. return $result ['access_token'];
  631. fclose ( $fp );
  632. exit ();
  633. }
  634. return $arr ['access_token'];
  635. fclose ( $fp );
  636. exit ();
  637. }
  638. $result = $this->postCurl ( $url, $option, $head = 0 );
  639. $result = json_decode($result,true);
  640. $result ['expires_in'] = $result ['expires_in'] + time ();
  641. $fp = @fopen ( "easemob.txt", 'w' );
  642. @fwrite ( $fp, serialize ( $result ) );
  643. return $result ['access_token'];
  644. fclose ( $fp );
  645. } */
  646. private function postCurl($url, $option, $header = 0, $type = 'POST')
  647. {
  648. $curl = curl_init (); // 启动一个CURL会话
  649. curl_setopt ( $curl, CURLOPT_URL, $url ); // 要访问的地址
  650. curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE ); // 对认证证书来源的检查
  651. curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, FALSE ); // 从证书中检查SSL加密算法是否存在
  652. curl_setopt ( $curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)' ); // 模拟用户使用的浏览器
  653. if (! empty ( $option ))
  654. {
  655. $options = json_encode ( $option );
  656. curl_setopt ( $curl, CURLOPT_POSTFIELDS, $options ); // Post提交的数据包
  657. }
  658. curl_setopt ( $curl, CURLOPT_TIMEOUT, 30 ); // 设置超时限制防止死循环
  659. if(is_object($header) || is_array($header))
  660. {
  661. curl_setopt($curl, CURLOPT_HTTPHEADER, $header); // 设置HTTP头
  662. }
  663. curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 获取的信息以文件流的形式返回
  664. curl_setopt ( $curl, CURLOPT_CUSTOMREQUEST, $type );
  665. $result = curl_exec ( $curl ); // 执行操作
  666. //$res = object_array ( json_decode ( $result ) );
  667. //$res ['status'] = curl_getinfo ( $curl, CURLINFO_HTTP_CODE );
  668. //pre ( $res );
  669. curl_close ( $curl ); // 关闭CURL会话
  670. return $result;
  671. }
  672. //postCurl方法
  673. public function curl($url, $body, $header = array(), $method = "POST")
  674. {
  675. array_push($header, 'Accept:application/json');
  676. array_push($header, 'Content-Type:application/json');
  677. //array_push($header, 'http:multipart/form-data');
  678. $ch = curl_init();//启动一个curl会话
  679. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
  680. curl_setopt($ch, CURLOPT_URL, $url);
  681. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  682. //curl_setopt($ch, $method, 1);
  683. switch ($method)
  684. {
  685. case "GET" :
  686. curl_setopt($ch, CURLOPT_HTTPGET, true);
  687. break;
  688. case "POST":
  689. curl_setopt($ch, CURLOPT_POST,true);
  690. break;
  691. case "PUT" :
  692. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  693. break;
  694. case "DELETE":
  695. curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
  696. break;
  697. }
  698. curl_setopt($ch, CURLOPT_USERAGENT, 'SSTS Browser/1.0');
  699. curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
  700. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  701. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);//原先是FALSE,可改为2
  702. if (isset($body{3}) > 0)
  703. {
  704. curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
  705. }
  706. if (count($header) > 0)
  707. {
  708. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  709. }
  710. $ret = curl_exec($ch);
  711. $err = curl_error($ch);
  712. curl_close($ch);
  713. //clear_object($ch);
  714. //clear_object($body);
  715. //clear_object($header);
  716. if ($err)
  717. {
  718. return $err;
  719. }
  720. return $ret;
  721. }
  722. }