ReportPayload.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. class ReportPayload {
  3. private static $EFFECTIVE_TIME_UNIT = array('HOUR', 'DAY', 'MONTH');
  4. private static $LIMIT_KEYS = array('X-Rate-Limit-Limit'=>'rateLimitLimit', 'X-Rate-Limit-Remaining'=>'rateLimitRemaining', 'X-Rate-Limit-Reset'=>'rateLimitReset');
  5. const REPORT_URL = 'https://report.jpush.cn/v3/received';
  6. const MESSAGES_URL = 'https://report.jpush.cn/v3/messages';
  7. const USERS_URL = 'https://report.jpush.cn/v3/users';
  8. private $client;
  9. /**
  10. * ReportPayload constructor.
  11. * @param $client JPush
  12. */
  13. public function __construct($client)
  14. {
  15. $this->client = $client;
  16. }
  17. public function getReceived($msgIds) {
  18. $queryParams = '?msg_ids=';
  19. if (is_array($msgIds) && count($msgIds) > 0) {
  20. $isFirst = true;
  21. foreach ($msgIds as $msgId) {
  22. if ($isFirst) {
  23. $queryParams .= $msgId;
  24. $isFirst = false;
  25. } else {
  26. $queryParams .= ',';
  27. $queryParams .= $msgId;
  28. }
  29. }
  30. } else if (is_string($msgIds)) {
  31. $queryParams .= $msgIds;
  32. } else {
  33. throw new InvalidArgumentException("Invalid msg_ids");
  34. }
  35. $url = ReportPayload::REPORT_URL . $queryParams;
  36. return $this->__request($url);
  37. }
  38. public function getMessages($msgIds) {
  39. $queryParams = '?msg_ids=';
  40. if (is_array($msgIds) && count($msgIds) > 0) {
  41. $isFirst = true;
  42. foreach ($msgIds as $msgId) {
  43. if ($isFirst) {
  44. $queryParams .= $msgId;
  45. $isFirst = false;
  46. } else {
  47. $queryParams .= ',';
  48. $queryParams .= $msgId;
  49. }
  50. }
  51. } else if (is_string($msgIds)) {
  52. $queryParams .= $msgIds;
  53. } else {
  54. throw new InvalidArgumentException("Invalid msg_ids");
  55. }
  56. $url = ReportPayload::MESSAGES_URL . $queryParams;
  57. return $this->__request($url);
  58. }
  59. public function getUsers($time_unit, $start, $duration) {
  60. $time_unit = strtoupper($time_unit);
  61. if (!in_array($time_unit, self::$EFFECTIVE_TIME_UNIT)) {
  62. throw new InvalidArgumentException('Invalid time unit');
  63. }
  64. $url = ReportPayload::USERS_URL . '?time_unit=' . $time_unit . '&start=' . $start . '&duration=' . $duration;
  65. return $this->__request($url);
  66. }
  67. private function __request($url) {
  68. $response = $this->client->_request($url, JPush::HTTP_GET);
  69. if($response['http_code'] === 200) {
  70. $body = array();
  71. $body['data'] = (array)json_decode($response['body']);
  72. $headers = $response['headers'];
  73. if (is_array($headers)) {
  74. $limit = array();
  75. foreach (self::$LIMIT_KEYS as $key => $value) {
  76. if (array_key_exists($key, $headers)) {
  77. $limit[$value] = $headers[$key];
  78. }
  79. }
  80. if (count($limit) > 0) {
  81. $body['limit'] = (object)$limit;
  82. }
  83. return (object)$body;
  84. }
  85. return $body;
  86. } else {
  87. throw new APIRequestException($response);
  88. }
  89. }
  90. }