umeng.php 6.1 KB

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