umeng.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: dell
  5. * Date: 2016/4/15
  6. * Time: 16:27
  7. */
  8. class msg_type extends SplEnum
  9. {
  10. const __default = self::unicast;
  11. const unicast = "unicast";
  12. const listcast = "listcast";
  13. const filecast = "filecast";
  14. const broadcast = "broadcast";
  15. const groupcast = "groupcast";
  16. const customizedcast = "customizedcast";
  17. const alias = "alias";
  18. const file_id = "file_id";
  19. }
  20. class status_code extends SplEnum
  21. {
  22. const __default = self::OK;
  23. const OK = 200;
  24. const CREATED = 201;
  25. const ACCEPTED = 202;
  26. const BAD_REQUEST = 400;
  27. const UNAUTHORIZED = 401;
  28. const FORBIDDEN = 403;
  29. const NOT_FOUND = 404;
  30. const INTERNAL_SERVICE_ERROR = 500;
  31. }
  32. class display_type extends SplEnum
  33. {
  34. const __default = self::notification;
  35. const notification = "notification";
  36. const message = "message";
  37. }
  38. /**
  39. * Class upush
  40. *
  41. * 友盟推送内容
  42. */
  43. class upush
  44. {
  45. const PUSH_URL = "http://msg.umeng.com/api/send";
  46. const APP_MASTER_SECRET = "r6w2a8z9x8zonh7qmk8ds2fvypu02wpj";
  47. const APPKEY = "5631efd4e0f55a8770000027";
  48. private $type;
  49. private $alias_type;
  50. private $alias;
  51. private $ticker;
  52. private $title;
  53. private $text;
  54. public function __construct()
  55. {
  56. $type = msg_type::unicast;
  57. $ticker = "ticker";
  58. $title = "title";
  59. $text = "text";
  60. }
  61. /**
  62. * post发送数据
  63. *
  64. * @param $url
  65. * @param $data_string
  66. * @return array
  67. */
  68. public function http_post_data($url, $data_string)
  69. {
  70. $ch = curl_init();
  71. curl_setopt($ch, CURLOPT_POST, 1);
  72. curl_setopt($ch, CURLOPT_URL, $url);
  73. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  74. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  75. 'Content-Type: application/json; charset=utf-8',
  76. 'Content-Length: ' . strlen($data_string))
  77. );
  78. ob_start();
  79. curl_exec($ch);
  80. $return_content = ob_get_contents();
  81. ob_end_clean();
  82. $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  83. return array($return_code, $return_content);
  84. }
  85. /**
  86. * 生成签名
  87. *
  88. * @param $http_method
  89. * @param $url
  90. * @param $post_body
  91. * @param $app_master_secret
  92. * @return string
  93. */
  94. private function make_sign($http_method, $url, $post_body, $app_master_secret)
  95. {
  96. return strtolower(md5($http_method . $url . json_encode($post_body) . self::APP_MASTER_SECRET));
  97. }
  98. // android 打包
  99. private function android_pack()
  100. {
  101. $pack = array();
  102. $pack['appkey'] = self::APPKEY;
  103. $pack['timestamp'] = time();
  104. $pack['type'] = $this->type;
  105. $pack['device_tokens'] = "Aj9AympPsCha5zmPhrV0DbnOZJYF0pqKY5jdKvFy_Hbu";
  106. $pack['alias_type'] = "";
  107. $pack['alias'] = "";
  108. $pack['file_id'] = "";
  109. $pack['filter'] = "";
  110. $payload = array();
  111. $body = array();
  112. $body['ticker'] = $this->ticker;
  113. $body['title'] = $this->title;
  114. $body['text'] = $this->text;
  115. $body['icon'] = "";
  116. $body['largeIcon'] = "";
  117. $body['img'] = "";
  118. $body['sound'] = "";
  119. $body['builder_id'] = "";
  120. $body['play_vibrate'] = "true";
  121. $body['play_lights'] = "true";
  122. $body['play_sound'] = "true";
  123. $body['after_open'] = "go_app";
  124. $body['url'] = "";
  125. $body['activity'] = "";
  126. $body['custom'] = "";
  127. $extra = array();
  128. $extra['key1'] = "key1";
  129. $extra['key1'] = "key2";
  130. $extra['key1'] = "key3";
  131. $payload['display_type'] = display_type::message;
  132. $payload['body'] = $body;
  133. $payload['extra'] = $extra;
  134. $pack['payload'] = $payload;
  135. $policy = array();
  136. $policy['start_time'] = "";
  137. $policy['expire_time'] = "";
  138. $policy['max_send_num'] = "100";
  139. $policy['out_biz_no'] = "10001";
  140. // $pack['policy'] = $policy;
  141. $pack['production_mode'] = "false";
  142. $pack['description'] = "";
  143. $pack['thirdparty_id'] = "";
  144. return $pack;
  145. }
  146. // ios 打包
  147. private function ios_pack()
  148. {
  149. }
  150. // android 推送
  151. public function android_push()
  152. {
  153. $pack_body = self::android_pack();
  154. $url = self::PUSH_URL . '?sign=' . self::make_sign("POST", self::PUSH_URL, $pack_body, self::APP_MASTER_SECRET);
  155. return $this->http_post_data($url, json_encode($pack_body));
  156. }
  157. // ios 推送
  158. public function ios_push()
  159. {
  160. }
  161. }