umeng.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: dell
  5. * Date: 2016/4/15
  6. * Time: 16:27
  7. */
  8. // 发送类型,安卓还是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 $devices_token;
  63. // 通知展示内容
  64. private $ticker;
  65. private $title;
  66. private $text;
  67. private $icon;
  68. private $largeIcon;
  69. private $img;
  70. private $sound;
  71. public function __construct()
  72. {
  73. $platform = platform_type::android;
  74. $type = msg_type::unicast;
  75. $ticker = "ticker";
  76. $title = "title";
  77. $text = "text";
  78. }
  79. // 设置属性
  80. private function __set($property_name, $value)
  81. {
  82. $this->$property_name = $value;
  83. }
  84. // 获取属性
  85. private function __get($property_name)
  86. {
  87. if (isset($this->$property_name)) {
  88. return ($this->$property_name);
  89. } else {
  90. return (NULL);
  91. }
  92. }
  93. /**
  94. * post发送数据
  95. *
  96. * @param $url
  97. * @param $data_string
  98. * @return array
  99. */
  100. public function http_post_data($url, $data_string)
  101. {
  102. $ch = curl_init();
  103. curl_setopt($ch, CURLOPT_POST, 1);
  104. curl_setopt($ch, CURLOPT_URL, $url);
  105. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  106. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  107. 'Content-Type: application/json; charset=utf-8',
  108. 'Content-Length: ' . strlen($data_string))
  109. );
  110. ob_start();
  111. curl_exec($ch);
  112. $return_content = ob_get_contents();
  113. ob_end_clean();
  114. $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  115. return array($return_code, $return_content);
  116. }
  117. /**
  118. * 生成签名
  119. *
  120. * @param $http_method
  121. * @param $url
  122. * @param $post_body
  123. * @param $app_master_secret
  124. * @return string
  125. */
  126. private function make_sign($http_method, $url, $post_body, $app_master_secret)
  127. {
  128. return strtolower(md5($http_method . $url . json_encode($post_body) . $app_master_secret));
  129. }
  130. // android 打包
  131. private function android_pack()
  132. {
  133. $pack = array();
  134. $pack['appkey'] = self::APPKEY;
  135. $pack['timestamp'] = time();
  136. $pack['type'] = msg_type::unicast;
  137. $pack['device_tokens'] = "Aj9AympPsCha5zmPhrV0DbnOZJYF0pqKY5jdKvFy_Hbu";
  138. $pack['alias_type'] = "";
  139. $pack['alias'] = "";
  140. $pack['file_id'] = "";
  141. $pack['filter'] = "";
  142. $payload = array();
  143. $body = array();
  144. $body['ticker'] = $this->ticker;
  145. $body['title'] = $this->title;
  146. $body['text'] = $this->text;
  147. $body['icon'] = $this->icon;
  148. $body['largeIcon'] = $this->largeIcon;
  149. $body['img'] = $this->img;
  150. $body['sound'] = $this->sound;
  151. $body['builder_id'] = "";
  152. $body['play_vibrate'] = "true";
  153. $body['play_lights'] = "true";
  154. $body['play_sound'] = "true";
  155. $body['after_open'] = "go_app";
  156. $body['url'] = "";
  157. $body['activity'] = "";
  158. $body['custom'] = "";
  159. $extra = array();
  160. $extra['key1'] = "key1";
  161. $extra['key1'] = "key2";
  162. $extra['key1'] = "key3";
  163. $payload['display_type'] = display_type::notification;
  164. $payload['body'] = $body;
  165. $payload['extra'] = $extra;
  166. $pack['payload'] = $payload;
  167. $policy = array();
  168. $policy['start_time'] = "";
  169. $policy['expire_time'] = "";
  170. $policy['max_send_num'] = "100";
  171. $policy['out_biz_no'] = "10001";
  172. // $pack['policy'] = $policy;
  173. $pack['production_mode'] = "false";
  174. $pack['description'] = "";
  175. $pack['thirdparty_id'] = "";
  176. return $pack;
  177. }
  178. // ios 推送
  179. public function android_push()
  180. {
  181. $pack_body = self::android_pack();
  182. $url = self::PUSH_URL . '?sign=' . self::make_sign("POST", self::PUSH_URL, $pack_body, self::APP_MASTER_SECRET);
  183. return $this->http_post_data($url, json_encode($pack_body));
  184. }
  185. // ios 打包
  186. private function ios_pack()
  187. {
  188. }
  189. // ios 推送
  190. public function ios_push()
  191. {
  192. }
  193. }