tpns.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. <?php
  2. namespace tpns {
  3. const GUANGZHOU = "api.tpns.tencent.com";
  4. const SINGAPORE = "api.tpns.sgp.tencent.com";
  5. const HONGKONG = "api.tpns.hk.tencent.com";
  6. const SHANGHAI = "api.tpns.sh.tencent.com";
  7. //audience type
  8. const AUDIENCE_ALL = "all";
  9. const AUDIENCE_TAG = "tag";
  10. const AUDIENCE_TOKEN = "token";
  11. const AUDIENCE_TOKEN_LIST = "token_list";
  12. const AUDIENCE_ACCOUNT = "account";
  13. const AUDIENCE_ACCOUNT_LIST = "account_list";
  14. const AUDIENCE_ACCOUNT_PACKAGE = "package_account_push";
  15. const AUDIENCE_TOKEN_PACKAGE = "package_token_push";
  16. // tag operation type
  17. const TAG_OPERATOR_AND = "AND";
  18. const TAG_OPERATOR_OR = "OR";
  19. //deprecated
  20. // platform type
  21. const PLATFORM_ANDROID = "android";
  22. const PLATFORM_IOS = "ios";
  23. // message type
  24. const MESSAGE_NOTIFY = "notify";
  25. const MESSAGE_MESSAGE = "message";
  26. // environment type
  27. const ENVIRONMENT_PROD = "product";
  28. const ENVIRONMENT_DEV = "dev";
  29. class AndroidActionActivityAttr {
  30. public $if = 0;
  31. public $pf = 0;
  32. public function filter() {
  33. }
  34. }
  35. class AndroidActionBrowserAttr {
  36. public $url = "";
  37. public $confirm = 0;
  38. public function filter() {
  39. }
  40. }
  41. class AndroidAction {
  42. public $action_type = 1;
  43. public $activity = "";
  44. public $aty_attr; //AndroidActionActivityAttr
  45. public $browser; //AndroidActionBrowserAttr
  46. public $intent = "";
  47. public function filter() {
  48. if (isset($this->aty_attr) && $this->aty_attr != null) {
  49. if (method_exists($this->aty_attr, 'filter')) {
  50. $this->aty_attr->filter();
  51. }
  52. } else {
  53. unset($this->aty_attr);
  54. }
  55. if (isset($this->browser) && $this->browser != null) {
  56. if (method_exists($this->browser, 'filter')) {
  57. $this->browser->filter();
  58. }
  59. } else {
  60. unset($this->browser);
  61. }
  62. }
  63. }
  64. class AndroidMessage {
  65. public $n_ch_id = "";
  66. public $n_ch_name = "";
  67. public $xm_ch_id = "";
  68. public $hw_ch_id = "";
  69. public $oppo_ch_id = "";
  70. public $vivo_ch_id = "";
  71. public $builder_id = 0;
  72. public $badge_type = -1;
  73. public $ring = 1;
  74. public $ring_raw = "";
  75. public $vibrate = 1;
  76. public $lights = 1;
  77. public $clearable = 1;
  78. public $icon_type = 0;
  79. public $icon_res = "";
  80. public $style_id = 0;
  81. public $small_icon = "";
  82. public $action; //AndroidAction
  83. public $custom_content = "";
  84. public $show_type = 2;
  85. public $icon_color = 0;
  86. public function filter() {
  87. if (isset($this->action) && $this->action != null) {
  88. if (method_exists($this->action, 'filter')) {
  89. $this->action->filter();
  90. }
  91. } else {
  92. unset($this->action);
  93. }
  94. }
  95. }
  96. class Aps {
  97. public $alert;
  98. public $badge_type = 0;
  99. public $category = "";
  100. public $content_available = 0; //json to content-available
  101. public $sound = "";
  102. public $mutable_content = 1; //json to mutable-content
  103. public function filter() {
  104. if (isset($this->alert) && $this->alert == null) {
  105. unset($this->alert);
  106. }
  107. }
  108. }
  109. class iOSMessage {
  110. public $aps; //Aps
  111. public $custom = "";
  112. public function filter () {
  113. if (isset($this->aps) && $this->aps != null) {
  114. if (method_exists($this->aps, 'filter')) {
  115. $this->aps->filter();
  116. }
  117. } else {
  118. unset($this->aps);
  119. }
  120. }
  121. }
  122. class TagItem {
  123. public $tags; //array of string
  124. public $is_not = false;
  125. public $tags_operator = "";
  126. public $items_operator = "";
  127. public $tag_type = "";
  128. public function filter () {
  129. if (isset($this->tags) && $this->tags == null) {
  130. unset($this->tags);
  131. }
  132. }
  133. }
  134. class TagRule {
  135. public $tag_items; //array of TagItem
  136. public $is_not = false;
  137. public $operator = "";
  138. public function filter () {
  139. if (isset($this->tag_items) && $this->tag_items == null) {
  140. unset($this->tag_items);
  141. }
  142. }
  143. }
  144. class AcceptTime {
  145. public $hour = "";
  146. public $min = "";
  147. public function filter() {
  148. }
  149. }
  150. class AcceptTimeRange {
  151. public $start; //AcceptTime
  152. public $end; //AcceptTime
  153. public function filter () {
  154. if (isset($this->start) && $this->start != null) {
  155. if (method_exists($this->start, 'filter')) {
  156. $this->start->filter();
  157. }
  158. } else {
  159. unset($this->start);
  160. }
  161. if (isset($this->end) && $this->end != null) {
  162. if (method_exists($this->end, 'filter')) {
  163. $this->end->filter();
  164. }
  165. } else {
  166. unset($this->end);
  167. }
  168. }
  169. }
  170. class Message {
  171. public $title = "";
  172. public $content = "";
  173. public $accept_time; //array of AcceptTimeRange
  174. public $thread_id = "";
  175. public $thread_sumtext = "";
  176. public $xg_media_resources = "";
  177. public $xg_media_audio_resources = "";
  178. public $android; //AndroidMessage
  179. public $ios; //iOSMessage
  180. public function filter() {
  181. if (isset($this->accept_time) && $this->accept_time == null) {
  182. unset($this->accept_time);
  183. }
  184. if (isset($this->android) && $this->android != null) {
  185. if (method_exists($this->android, 'filter')){
  186. $this->android->filter();
  187. }
  188. } else {
  189. unset($this->android);
  190. }
  191. if (isset($this->ios) && $this->ios != null) {
  192. if (method_exists($this->ios, 'filter')) {
  193. $this->ios->filter();
  194. }
  195. } else {
  196. unset($this->ios);
  197. }
  198. }
  199. }
  200. class ChannelRule {
  201. public $channel = "";
  202. public $disable = false;
  203. public function filter() {
  204. }
  205. }
  206. class LoopParam {
  207. public $startDate = "";
  208. public $endDate = "";
  209. public $loopType = 0;
  210. public $loopDayIndexs; //array of uint32
  211. public $DayTimes; //array of string
  212. public function filter() {
  213. if (isset($this->loopDayIndexs) && $this->loopDayIndexs == null) {
  214. unset($this->loopDayIndexs);
  215. }
  216. if (isset($this->DayTimes) && $this->DayTimes == null) {
  217. unset($this->DayTimes);
  218. }
  219. }
  220. };
  221. class Request {
  222. public $audience_type = "";
  223. public $platform = "";
  224. public $message; // Message
  225. public $message_type = "";
  226. public $tag_rules; // array of TagRule
  227. public $token_list; // array of string
  228. public $account_list; // array of string
  229. public $environment = "";
  230. public $upload_id = 0;
  231. public $expire_time = 259200;
  232. public $send_time = "";
  233. public $multi_pkg = false;
  234. public $plan_id = "";
  235. public $account_push_type = 0;
  236. public $account_type = 0;
  237. public $collapse_id = 0;
  238. public $push_speed = 0;
  239. public $tpns_online_push_type = 0;
  240. public $ignore_invalid_token = 0;
  241. public $force_collapse = false;
  242. public $channel_rules; //array of ChannelRule
  243. public $loop_param; //LoopParam
  244. public function filter() {
  245. if (isset($this->message) && $this->message != null) {
  246. if (method_exists($this->message, 'filter')){
  247. $this->message->filter();
  248. }
  249. } else {
  250. unset($this->message);
  251. }
  252. if (isset($this->tag_rules) && $this->tag_rules == null) {
  253. unset($this->tag_rules);
  254. }
  255. if (isset($this->token_list) && $this->token_list == null) {
  256. unset($this->token_list);
  257. }
  258. if (isset($this->account_list) && $this->account_list == null) {
  259. unset($this->account_list);
  260. }
  261. if (isset($this->channel_rules) && $this->channel_rules == null) {
  262. unset($this->channel_rules);
  263. }
  264. if (isset($this->loop_param) && $this->loop_param != null) {
  265. if (method_exists($this->loop_param, 'filter')) {
  266. $this->loop_param->filter();
  267. }
  268. } else {
  269. unset($this->loop_param);
  270. }
  271. }
  272. public function Validate() {
  273. if (empty($this->audience_type)) {
  274. throw new \Exception("audience_type is not set");
  275. }
  276. if ($this->audience_type != AUDIENCE_ALL &&
  277. $this->audience_type != AUDIENCE_TAG &&
  278. $this->audience_type != AUDIENCE_TOKEN &&
  279. $this->audience_type != AUDIENCE_TOKEN_LIST &&
  280. $this->audience_type != AUDIENCE_ACCOUNT &&
  281. $this->audience_type != AUDIENCE_ACCOUNT_LIST &&
  282. $this->audience_type != AUDIENCE_ACCOUNT_PACKAGE &&
  283. $this->audience_type != AUDIENCE_TOKEN_PACKAGE) {
  284. throw new \Exception ("invalid audience_type: ".$this->audience_type);
  285. }
  286. if ($this->audience_type == AUDIENCE_TOKEN || $this->audience_type == AUDIENCE_TOKEN_LIST) {
  287. if (empty($this->token_list)) {
  288. throw new \Exception ("empty token_list");
  289. }
  290. if (!is_array($this->token_list)) {
  291. throw new \Exception ("token_list need to be array");
  292. }
  293. }
  294. if ($this->audience_type == AUDIENCE_ACCOUNT || $this->audience_type == AUDIENCE_ACCOUNT_LIST) {
  295. if (empty($this->account_list)) {
  296. throw new \Exception ("empty account_list");
  297. }
  298. if (!is_array($this->account_list)) {
  299. throw new \Exception ("account_list need to be array");
  300. }
  301. }
  302. if ($this->audience_type == AUDIENCE_TAG) {
  303. if (empty($this->tag_rules)) {
  304. throw new \Exception ("empty tag_rules");
  305. }
  306. if (!is_array($this->tag_rules)) {
  307. throw new \Exception ("tag_rules need to be array");
  308. }
  309. }
  310. //if (empty($this->platform)) {
  311. // throw new \Exception("empty platform");
  312. //}
  313. //if ($this->platform != PLATFORM_ANDROID && $this->platform != PLATFORM_IOS) {
  314. // throw new \Exception("invalid platform: " . $this->platform);
  315. //}
  316. if ($this->message == null) {
  317. throw new \Exception("empty message");
  318. }
  319. if (empty($this->message_type)) {
  320. throw new \Exception("empty message_type");
  321. }
  322. if ($this->message_type != MESSAGE_NOTIFY && $this->message_type != MESSAGE_MESSAGE) {
  323. throw new \Exception("invalid message_type: " . $this->message_type);
  324. }
  325. //if ($this->platform == PLATFORM_IOS) {
  326. if ($this->message->ios != null) {
  327. if (empty($this->environment)) {
  328. throw new \Exception("empty environment");
  329. }
  330. if ($this->environment != ENVIRONMENT_PROD && $this->environment != ENVIRONMENT_DEV) {
  331. throw new \Exception("invalid environment: " . $this->environment);
  332. }
  333. }
  334. if (isset($this->channel_rules)) {
  335. if (!is_array($this->channel_rules)) {
  336. throw new \Exception ("channel_rules need to be array");
  337. }
  338. }
  339. }
  340. public function Marshal() {
  341. $this->filter();
  342. //if ($this->platform == PLATFORM_ANDROID) {
  343. // unset($this->message->ios);
  344. //}
  345. //if ($this->platform == PLATFORM_IOS) {
  346. // unset($this->message->android);
  347. if (isset($this->message) && $this->message != null) {
  348. if (isset($this->message->ios) && $this->message->ios != null) {
  349. if (isset($this->message->ios->aps) && $this->message->ios->aps != null) {
  350. $aps = $this->message->ios->aps;
  351. if (isset($aps->content_available) && $aps->content_available != null &&
  352. isset($aps->mutable_content) && $aps->mutable_content != null) {
  353. $this->message->ios->aps = array(
  354. "alert" => $aps->alert,
  355. "badge_type" => $aps->badge_type,
  356. "category" => $aps->category,
  357. "content-available" => $aps->content_available,
  358. "sound" => $aps->sound,
  359. "mutable-content" => $aps->mutable_content
  360. );
  361. }
  362. }
  363. }
  364. }
  365. //}
  366. $data = json_encode($this);
  367. return $data;
  368. }
  369. }
  370. // set audience type: AUDIENCE_ALL, AUDIENCE_TAG, AUDIENCE_TOKEN, AUDIENCE_TOKEN_LIST, AUDIENCE_ACCOUNT, AUDIENCE_ACCOUNT_LIST, AUDIENCE_ACCOUNT_PACKAGE, AUDIENCE_TOKEN_PACKAGE
  371. function WithAudienceType($type) {
  372. return function($r) use ($type) {
  373. $r->audience_type = $type;
  374. };
  375. }
  376. //deprecated
  377. // set platform: PLATFORM_ANDROID, PLATFORM_IOS
  378. function WithPlatform($platform) {
  379. return function($r) use ($platform) {
  380. $r->platform = $platform;
  381. };
  382. }
  383. //set message, type: Message
  384. function WithMessage($message) {
  385. return function($r) use ($message) {
  386. $r->message = $message;
  387. };
  388. }
  389. //set message title, type: string
  390. function WithTitle($title) {
  391. return function($r) use ($title) {
  392. if ($r->message == null) {
  393. $r->message = new Message;
  394. }
  395. $r->message->title = $title;
  396. };
  397. }
  398. //set message content, type: string
  399. function WithContent($content) {
  400. return function($r) use ($content) {
  401. if ($r->message == null) {
  402. $r->message = new Message;
  403. }
  404. $r->message->content = $content;
  405. };
  406. }
  407. //set message accept_time, type: array of AcceptTimeRange
  408. function WithAcceptTime($acceptTime) {
  409. return function($r) use ($acceptTime) {
  410. if ($r->message == null) {
  411. $r->message = new Message;
  412. }
  413. $r->message->accept_time = $acceptTime;
  414. };
  415. }
  416. //set message thread_id, type: string
  417. function WithThreadId($threadId) {
  418. return function($r) use ($threadId) {
  419. if ($r->message == null) {
  420. $r->message = new Message;
  421. }
  422. $r->message->thread_id = $threadId;
  423. };
  424. }
  425. //set message thread_sumtext, type: string
  426. function WithThreadSumText($threadSumText) {
  427. return function($r) use ($threadSumText) {
  428. if ($r->message == null) {
  429. $r->message = new Message;
  430. }
  431. $r->message->thread_sumtext = $threadSumText;
  432. };
  433. }
  434. //set message xg_media_resources, type string
  435. function WithXGMediaResources($xgMediaResources) {
  436. return function($r) use ($xgMediaResources) {
  437. if ($r->message == null) {
  438. $r->message = new Message;
  439. }
  440. $r->message->xg_media_resources = $xgMediaResources;
  441. };
  442. }
  443. //set message xg_media_audio_resources, type string
  444. function WithXGMediaAudioResources($xgMediaAudioResources) {
  445. return function($r) use ($xgMediaAudioResources) {
  446. if ($r->message == null) {
  447. $r->message = new Message;
  448. }
  449. $r->message->xg_media_audio_resources = $xgMediaAudioResources;
  450. };
  451. }
  452. //set message android, type AndroidMessage
  453. function WithAndroidMessage($android) {
  454. return function($r) use ($android) {
  455. if ($r->message == null) {
  456. $r->message = new Message;
  457. }
  458. $r->message->android = $android;
  459. };
  460. }
  461. //set message ios, type iOSMessage
  462. function WithIOSMessage($ios) {
  463. return function($r) use ($ios) {
  464. if ($r->message == null) {
  465. $r->message = new Message;
  466. }
  467. $r->message->ios = $ios;
  468. };
  469. }
  470. //set message_type, 'MESSAGE_NOTIFY' or 'MESSAGE_MESSAGE'
  471. function WithMessageType($type) {
  472. return function($r) use ($type) {
  473. $r->message_type = $type;
  474. };
  475. }
  476. //set tag_rules, type: array of TagRule
  477. function WithTagRules($tagRules) {
  478. return function($r) use ($tagRules) {
  479. $r->tag_rules = $tagRules;
  480. };
  481. }
  482. //set token_list, type: array of string
  483. function WithTokenList($tokenList) {
  484. return function($r) use ($tokenList) {
  485. $r->token_list = $tokenList;
  486. };
  487. }
  488. //set account_list, type: array of string
  489. function WithAccountList($accountList) {
  490. return function($r) use ($accountList) {
  491. $r->account_list = $accountList;
  492. };
  493. }
  494. //set environment, only for iOS, 'ENVIRONMENT_PROD' or 'ENVIRONMENT_DEV'
  495. function WithEnvironment($env) {
  496. return function($r) use ($env) {
  497. $r->environment = $env;
  498. };
  499. }
  500. //set upload_id, type: int
  501. function WithUploadId($uploadId) {
  502. return function($r) use ($uploadId) {
  503. $r->upload_id = $uploadId;
  504. };
  505. }
  506. //set expire_time, type: int
  507. function WithExpireTime($expireTime) {
  508. return function($r) use ($expireTime) {
  509. $r->expire_time = $expireTime;
  510. };
  511. }
  512. //set send_time, type: string
  513. function WithSendTime($sendtime) {
  514. return function($r) use ($sendtime) {
  515. $r->send_time = $sendtime;
  516. };
  517. }
  518. //set multi_pkg, type: boolean
  519. function WithMultiPkg($multiPkg) {
  520. return function($r) use ($multiPkg) {
  521. $r->multi_pkg = $multiPkg;
  522. };
  523. }
  524. //set plan_id, type: string
  525. function WithPlanId($planId) {
  526. return function($r) use ($planId) {
  527. $r->plan_id = $planId;
  528. };
  529. }
  530. //set account_push_type, type: int
  531. function WithAccountPushType($type) {
  532. return function($r) use ($type) {
  533. $r->account_push_type = $type;
  534. };
  535. }
  536. //set account_type, type: int
  537. function WithAccountType($type) {
  538. return function($r) use ($type) {
  539. $r->account_type = $type;
  540. };
  541. }
  542. //set collapse_id, type: int
  543. function WithCollapseId($collapseId) {
  544. return function($r) use ($collapseId) {
  545. $r->collapse_id = $collapseId;
  546. };
  547. }
  548. //set push_speed, type: int
  549. function WithPushSpeed($speed) {
  550. return function($r) use ($speed) {
  551. $r->push_speed = $speed;
  552. };
  553. }
  554. //set tpns_online_push_type, type int
  555. function WithTpnsOnlinePushType($type) {
  556. return function($r) use ($type) {
  557. $r->tpns_online_push_type = $type;
  558. };
  559. }
  560. //set ignore_invalid_token, type: bool
  561. function WithIgnoreInvalidToken($type) {
  562. return function($r) use ($type) {
  563. $r->ignore_invalid_token = $type ? 1 : 0;
  564. };
  565. }
  566. //set force_collapse, type: bool
  567. function WithForceCollapse($force) {
  568. return function($r) use ($force) {
  569. $r->force_collapse = $force;
  570. };
  571. }
  572. //set channel_rules, type: array of ChannelRule
  573. function WithChannelRules($channelRules) {
  574. return function($r) use ($channelRules) {
  575. $r->channel_rules = $channelRules;
  576. };
  577. }
  578. //set loop_param, type: LoopParam
  579. function WithLoopParam($param) {
  580. return function($r) use ($param) {
  581. $r->loop_param = $param;
  582. };
  583. }
  584. //@param: WithXXX
  585. //php<5.6 not support ...$args
  586. function NewRequest() {
  587. $r= new Request;
  588. $num = func_num_args();
  589. $args = func_get_args();
  590. for ($i = 0; $i < $num; $i++) {
  591. $args[$i]($r);
  592. }
  593. return $r;
  594. }
  595. class Stub {
  596. public $host;
  597. public $sign;
  598. public function __construct($accessId, $secretKey, $host) {
  599. $this->host = $host;
  600. if (strpos($host, "http://") === false && strpos($host, "https://") === false) {
  601. $this->host = "https://" . $host;
  602. }
  603. $this->sign = base64_encode($accessId . ":" . $secretKey);
  604. }
  605. public function Push($request) {
  606. $request->Validate();
  607. $data = $request->Marshal();
  608. $headers = array("Content-type: application/json;charset='utf-8'", "Authorization: Basic " . $this->sign);
  609. $url = $this->host . "/v3/push/app";
  610. $options = array(
  611. CURLOPT_HTTPHEADER => $headers,
  612. CURLOPT_HEADER => 0,
  613. CURLOPT_SSL_VERIFYPEER => false,
  614. CURLOPT_SSL_VERIFYHOST => 0,
  615. CURLOPT_POST => 1,
  616. CURLOPT_POSTFIELDS => $data,
  617. CURLOPT_URL => $url,
  618. CURLOPT_RETURNTRANSFER => 1,
  619. CURLOPT_TIMEOUT => 10000
  620. );
  621. $ch = curl_init();
  622. curl_setopt_array($ch, $options);
  623. $ret = curl_exec($ch);
  624. $error = curl_error($ch);
  625. $info = curl_getinfo($ch);
  626. curl_close($ch);
  627. if ($error != "") {
  628. throw new \Exception($error);
  629. }
  630. $code = $info["http_code"];
  631. /*var_dump($code);
  632. die;*/
  633. if ($code != 200) {
  634. //throw new \Exception("status: " . $code . ", message: " . $ret);
  635. return array('ret_code'=>$code,'err_msg'=>$ret);
  636. }
  637. /*array(8) {
  638. ["seq"]=>
  639. int(0)
  640. ["push_id"]=>
  641. string(9) "572728156"
  642. ["ret_code"]=>
  643. int(0)
  644. ["environment"]=>
  645. string(7) "product"
  646. ["err_msg"]=>
  647. string(8) "NO_ERROR"
  648. ["err_msg_zh"]=>
  649. string(0) ""
  650. ["result"]=>
  651. string(2) "{}"
  652. ["invalid_targe_list"]=>
  653. array(0) {
  654. }
  655. }*/
  656. return json_decode($ret, 1);
  657. }
  658. public function UploadFile($file) {
  659. $headers = array("Content-type: multipart/form-data", "Authorization: Basic " . $this->sign);
  660. $url = $this->host . "/v3/push/package/upload";
  661. $cfile = new \CURLFile($file,'multipart/form-data',basename($file));
  662. $options = array(
  663. CURLOPT_HTTPHEADER => $headers,
  664. CURLOPT_HEADER => 0,
  665. CURLOPT_SSL_VERIFYPEER => false,
  666. CURLOPT_SSL_VERIFYHOST => 0,
  667. CURLOPT_POST => 1,
  668. CURLOPT_URL => $url,
  669. CURLOPT_RETURNTRANSFER => 1,
  670. CURLOPT_TIMEOUT => 10000,
  671. CURLOPT_POSTFIELDS => array("file" => $cfile)
  672. );
  673. $ch = curl_init();
  674. curl_setopt_array($ch, $options);
  675. $ret = curl_exec($ch);
  676. $error = curl_error($ch);
  677. $info = curl_getinfo($ch);
  678. curl_close($ch);
  679. if ($error != "") {
  680. throw new \Exception($error);
  681. }
  682. $code = $info["http_code"];
  683. if ($code != 200) {
  684. throw new \Exception("status: " . $code . ", message: " . $ret);
  685. }
  686. $data = json_decode($ret, 1);
  687. if ($data["retCode"] != 0) {
  688. throw new \Exception("upload error, retCode: " . $data["retCode"] . ", errMsg: " . $data["errMsg"]);
  689. }
  690. return $data["uploadId"];
  691. }
  692. }
  693. }