1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Pushok package. |
5
|
|
|
* |
6
|
|
|
* (c) Arthur Edamov <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Pushok; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Request |
16
|
|
|
* @package Pushok |
17
|
|
|
* |
18
|
|
|
* @see http://bit.ly/communicating-with-apns |
19
|
|
|
*/ |
20
|
|
|
class Request |
21
|
|
|
{ |
22
|
|
|
const APNS_DEVELOPMENT_SERVER = 'https://api.development.push.apple.com'; |
23
|
|
|
const APNS_PRODUCTION_SERVER = 'https://api.push.apple.com'; |
24
|
|
|
const APNS_PORT = 443; |
25
|
|
|
const APNS_PATH_SCHEMA = '/3/device/{token}'; |
26
|
|
|
|
27
|
|
|
const HEADER_APNS_ID = 'apns-id'; |
28
|
|
|
const HEADER_APNS_EXPIRATION = 'apns-expiration'; |
29
|
|
|
const HEADER_APNS_PRIORITY = 'apns-priority'; |
30
|
|
|
const HEADER_APNS_TOPIC = 'apns-topic'; |
31
|
|
|
const HEADER_APNS_COLLAPSE_ID = 'apns-collapse-id'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Request headers. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $headers = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Curl options. |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
private $options = []; |
46
|
|
|
|
47
|
|
|
public function __construct(Notification $notification, $isProductionEnv) |
48
|
|
|
{ |
49
|
|
|
$url = $isProductionEnv ? $this->getProductionUrl($notification) : $this->getSandboxUrl($notification); |
50
|
|
|
|
51
|
|
|
if (!defined('CURL_HTTP_VERSION_2')) { |
52
|
|
|
define('CURL_HTTP_VERSION_2', 3); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->options = [ |
56
|
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2, |
57
|
|
|
CURLOPT_URL => $url, |
58
|
|
|
CURLOPT_PORT => self::APNS_PORT, |
59
|
|
|
CURLOPT_POST => true, |
60
|
|
|
CURLOPT_POSTFIELDS => $notification->getPayload()->toJson(), |
61
|
|
|
CURLOPT_RETURNTRANSFER => true, |
62
|
|
|
CURLOPT_TIMEOUT => 30, |
63
|
|
|
CURLOPT_HEADER => true, |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
$this->prepareApnsHeaders($notification); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Add curl options. |
71
|
|
|
* |
72
|
|
|
* @param int $key |
73
|
|
|
* @param $value |
74
|
|
|
* @return Request |
75
|
|
|
*/ |
76
|
|
|
public function addOption(int $key, $value): Request |
77
|
|
|
{ |
78
|
|
|
$this->options[$key] = $value; |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Add curl options. |
85
|
|
|
* |
86
|
|
|
* @param array $options |
87
|
|
|
* @return Request |
88
|
|
|
*/ |
89
|
|
|
public function addOptions(array $options): Request |
90
|
|
|
{ |
91
|
|
|
$this->headers = array_merge($this->options, $options); |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getOptions() |
97
|
|
|
{ |
98
|
|
|
return $this->options; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Add request header. |
103
|
|
|
* |
104
|
|
|
* @param string $header |
105
|
|
|
*/ |
106
|
|
|
public function addHeader(string $header) |
107
|
|
|
{ |
108
|
|
|
$this->headers[] = $header; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Add request headers. |
113
|
|
|
* |
114
|
|
|
* @param Notification[] $headers |
115
|
|
|
*/ |
116
|
|
|
public function addHeaders(array $headers) |
117
|
|
|
{ |
118
|
|
|
$this->headers = array_merge($this->headers, $headers); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get request headers. |
123
|
|
|
* |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
public function getHeaders(): array |
127
|
|
|
{ |
128
|
|
|
return $this->headers; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
private function getProductionUrl(Notification $notification) |
132
|
|
|
{ |
133
|
|
|
return self::APNS_PRODUCTION_SERVER . $this->getUrlPath($notification); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
private function getSandboxUrl(Notification $notification) |
137
|
|
|
{ |
138
|
|
|
return self::APNS_DEVELOPMENT_SERVER . $this->getUrlPath($notification); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
private function getUrlPath(Notification $notification) |
142
|
|
|
{ |
143
|
|
|
return str_replace("{token}", $notification->getDeviceToken(), self::APNS_PATH_SCHEMA); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
private function prepareApnsHeaders(Notification $notification) |
147
|
|
|
{ |
148
|
|
|
if ($notification->getId()) { |
|
|
|
|
149
|
|
|
$this->headers[] = self::HEADER_APNS_ID . ': ' . $notification->getId(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if ($notification->getExpirationAt()) { |
153
|
|
|
$this->headers[] = self::HEADER_APNS_EXPIRATION . ': ' . $notification->getExpirationAt(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if ($notification->getPriority()) { |
|
|
|
|
157
|
|
|
$this->headers[] = self::HEADER_APNS_PRIORITY . ': ' . $notification->getPriority(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if ($notification->getCollapseId()) { |
|
|
|
|
161
|
|
|
$this->headers[] = self::HEADER_APNS_COLLAPSE_ID . ': ' . $notification->getCollapseId(); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: