|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* (c) Alexander Zhukov <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Zbox\UnifiedPush\NotificationService\MPNS; |
|
11
|
|
|
|
|
12
|
|
|
use Zbox\UnifiedPush\NotificationService\ServiceClientBase; |
|
13
|
|
|
use Zbox\UnifiedPush\Exception\ClientException; |
|
14
|
|
|
use Buzz\Browser; |
|
15
|
|
|
use Buzz\Client\MultiCurl; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class ServiceClient |
|
19
|
|
|
* @package Zbox\UnifiedPush\NotificationService\MPNS |
|
20
|
|
|
*/ |
|
21
|
|
|
class ServiceClient extends ServiceClientBase |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Initializing HTTP client |
|
25
|
|
|
* |
|
26
|
|
|
* @return $this |
|
27
|
|
|
*/ |
|
28
|
|
|
protected function createClient() |
|
29
|
|
|
{ |
|
30
|
|
|
$client = new MultiCurl(); |
|
31
|
|
|
|
|
32
|
|
|
$credentials = $this->getCredentials(); |
|
33
|
|
|
$isAuthenticated = $credentials->isAuthenticated(); |
|
34
|
|
|
|
|
35
|
|
|
$client->setVerifyPeer($isAuthenticated); |
|
36
|
|
|
|
|
37
|
|
|
if ($isAuthenticated) { |
|
38
|
|
|
$client->setOption(CURLOPT_SSLCERT, $credentials->getCertificatePassPhrase()); |
|
39
|
|
|
$client->setOption(CURLOPT_SSLCERTPASSWD, $credentials->getCertificatePassPhrase()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$this->serviceClient = new Browser($client); |
|
43
|
|
|
|
|
44
|
|
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @throws ClientException |
|
49
|
|
|
* @return bool |
|
50
|
|
|
*/ |
|
51
|
|
|
public function sendRequest() |
|
52
|
|
|
{ |
|
53
|
|
|
$notification = $this->getNotificationOrThrowException(); |
|
54
|
|
|
|
|
55
|
|
|
try { |
|
56
|
|
|
$connection = $this->getClientConnection(); |
|
57
|
|
|
$serviceURL = $this->getServiceURL(); |
|
58
|
|
|
$url = str_replace('[TOKEN]', $notification['recipients'][0], $serviceURL['url']); |
|
59
|
|
|
|
|
60
|
|
|
$headers = array(); |
|
61
|
|
|
$headers[] = 'Accept: application/*'; |
|
62
|
|
|
$headers[] = 'Content-Type: text/xml'; |
|
63
|
|
|
|
|
64
|
|
|
foreach ($notification['options'] as $key => $value) { |
|
65
|
|
|
$headers[] = $key . ': ' . $value; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$response = $connection->post($url, $headers, $notification['body']); |
|
69
|
|
|
$connection->getClient()->flush(); |
|
70
|
|
|
|
|
71
|
|
|
} catch (\Exception $e) { |
|
72
|
|
|
throw new ClientException($e->getMessage()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
new Response($response, $notification['recipients']); |
|
76
|
|
|
|
|
77
|
|
|
return true; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|