umeng.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: dell
  5. * Date: 2016/4/15
  6. * Time: 16:27
  7. */
  8. // 发送类型, android 或者 ios
  9. class platform_type extends SplEnum
  10. {
  11. const __default = self::unicast;
  12. const android = 1;
  13. const ios = 2;
  14. }
  15. // 消息类型
  16. class msg_type extends SplEnum
  17. {
  18. const __default = self::unicast;
  19. const unicast = "unicast";
  20. const listcast = "listcast";
  21. const filecast = "filecast";
  22. const broadcast = "broadcast";
  23. const groupcast = "groupcast";
  24. const customizedcast = "customizedcast";
  25. const alias = "alias";
  26. const file_id = "file_id";
  27. }
  28. class status_code extends SplEnum
  29. {
  30. const __default = self::OK;
  31. const OK = 200;
  32. const CREATED = 201;
  33. const ACCEPTED = 202;
  34. const BAD_REQUEST = 400;
  35. const UNAUTHORIZED = 401;
  36. const FORBIDDEN = 403;
  37. const NOT_FOUND = 404;
  38. const INTERNAL_SERVICE_ERROR = 500;
  39. }
  40. class display_type extends SplEnum
  41. {
  42. const __default = self::notification;
  43. const notification = "notification";
  44. const message = "message";
  45. }
  46. /**
  47. * Class upush
  48. *
  49. * 友盟推送内容
  50. */
  51. class upush
  52. {
  53. const PUSH_URL = "http://msg.umeng.com/api/send";
  54. const METHOD = "POST";
  55. const APP_MASTER_SECRET = "r6w2a8z9x8zonh7qmk8ds2fvypu02wpj";
  56. const APPKEY = "5631efd4e0f55a8770000027";
  57. private $platform;
  58. private $type;
  59. private $alias_type;
  60. private $alias;
  61. // 个人信息
  62. private $member_id = array(); // 必填选项
  63. private $devices_token = array();
  64. // 通知展示内容
  65. private $ticker;
  66. private $title;
  67. private $text;
  68. private $icon;
  69. private $largeIcon;
  70. private $img;
  71. private $sound;
  72. public function __construct()
  73. {
  74. $platform = platform_type::android;
  75. $type = msg_type::unicast;
  76. $ticker = "ticker";
  77. $title = "title";
  78. $text = "text";
  79. }
  80. // 设置属性
  81. private function __set($property_name, $value)
  82. {
  83. $this->$property_name = $value;
  84. }
  85. // 获取属性
  86. private function __get($property_name)
  87. {
  88. if (isset($this->$property_name)) {
  89. return ($this->$property_name);
  90. } else {
  91. return (NULL);
  92. }
  93. }
  94. /**
  95. * post发送数据
  96. *
  97. * @param $url
  98. * @param $data_string
  99. * @return array
  100. */
  101. public function http_post_data($url, $data_string)
  102. {
  103. $ch = curl_init();
  104. curl_setopt($ch, CURLOPT_POST, 1);
  105. curl_setopt($ch, CURLOPT_URL, $url);
  106. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  107. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  108. 'Content-Type: application/json; charset=utf-8',
  109. 'Content-Length: ' . strlen($data_string))
  110. );
  111. ob_start();
  112. curl_exec($ch);
  113. $return_content = ob_get_contents();
  114. ob_end_clean();
  115. $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  116. return array($return_code, $return_content);
  117. }
  118. /**
  119. * 生成签名
  120. *
  121. * @param $http_method
  122. * @param $url
  123. * @param $post_body
  124. * @param $app_master_secret
  125. * @return string
  126. */
  127. private function make_sign($http_method, $url, $post_body, $app_master_secret)
  128. {
  129. return strtolower(md5($http_method . $url . json_encode($post_body) . $app_master_secret));
  130. }
  131. // android 打包
  132. private function android_pack()
  133. {
  134. $pack = array();
  135. $pack['appkey'] = self::APPKEY;
  136. $pack['timestamp'] = time();
  137. $pack['type'] = msg_type::unicast;
  138. $pack['device_tokens'] = "Aj9AympPsCha5zmPhrV0DbnOZJYF0pqKY5jdKvFy_Hbu";
  139. // $pack['device_tokens'] = $this->devices_token;
  140. $pack['alias_type'] = "";
  141. $pack['alias'] = "";
  142. $pack['file_id'] = "";
  143. $pack['filter'] = "";
  144. $payload = array();
  145. $body = array();
  146. $body['ticker'] = $this->ticker;
  147. $body['title'] = $this->title;
  148. $body['text'] = $this->text;
  149. $body['icon'] = $this->icon;
  150. $body['largeIcon'] = $this->largeIcon;
  151. $body['img'] = $this->img;
  152. $body['sound'] = $this->sound;
  153. $body['builder_id'] = "";
  154. $body['play_vibrate'] = "true";
  155. $body['play_lights'] = "true";
  156. $body['play_sound'] = "true";
  157. $body['after_open'] = "go_app";
  158. $body['url'] = "";
  159. $body['activity'] = "";
  160. $body['custom'] = "";
  161. $extra = array();
  162. $extra['key1'] = "key1";
  163. $extra['key1'] = "key2";
  164. $extra['key1'] = "key3";
  165. $payload['display_type'] = display_type::message;
  166. $payload['body'] = $body;
  167. $payload['extra'] = $extra;
  168. $pack['payload'] = $payload;
  169. $policy = array();
  170. $policy['start_time'] = "";
  171. $policy['expire_time'] = "";
  172. $policy['max_send_num'] = "100";
  173. $policy['out_biz_no'] = "10001";
  174. // $pack['policy'] = $policy;
  175. $pack['production_mode'] = "false";
  176. $pack['description'] = "";
  177. $pack['thirdparty_id'] = "";
  178. return $pack;
  179. }
  180. private function pre_pack()
  181. {
  182. if (!empty($this->member_id)) {
  183. $result = Model()->table('device_binding')->where(array('member_id' => array('in', $this->member_id)))->select();
  184. foreach ($result as $value) {
  185. array_push($this->devices_token, $value['upush_device_token']);
  186. }
  187. return true;
  188. } else {
  189. return false;
  190. }
  191. }
  192. // android 推送
  193. public function android_push()
  194. {
  195. // $ret = $this->pre_pack();
  196. // if ($ret) {
  197. $pack_body = self::android_pack();
  198. $url = self::PUSH_URL . '?sign=' . self::make_sign("POST", self::PUSH_URL, $pack_body, self::APP_MASTER_SECRET);
  199. return $this->http_post_data($url, json_encode($pack_body));
  200. // } else {
  201. // return array(600, '参数输入不完整');
  202. // }
  203. }
  204. }