/
SuperfeedrClient.php
124 lines (107 loc) · 3.04 KB
/
SuperfeedrClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace DMS\Service\Superfeedr;
class SuperfeedrClient extends \GuzzleHttp\Client
{
/**
* @var string
*/
protected $hubSecret;
/**
* @param array $username
* @param $password
* @param $secret
* @param array $config
*/
public function __construct($username, $password, $secret, array $config = [])
{
$this->hubSecret = sha1($secret);
$defaultConfig = [
'base_url' => 'https://push.superfeedr.com',
'defaults' => [
'auth' => [$username, $password],
'debug' => fopen('php://output','w')
]
];
parent::__construct(array_merge_recursive($defaultConfig, $config));
}
/**
* @param $feed
* @param $callbackUrl
* @param string $format
* @return \GuzzleHttp\Message\ResponseInterface
*/
public function subscribeFeed($feed, $callbackUrl, $format = 'ATOM')
{
$options = [
'body' => [
'hub.mode' => 'subscribe',
'hub.topic' => $feed,
'hub.callback' => $callbackUrl,
'hub.secret' => $this->hubSecret,
'format' => $format,
]
];
return $this->post('/', $options);
}
/**
* @param string $feed
* @param string|null $callbackUrl
* @return \GuzzleHttp\Message\ResponseInterface
*/
public function unsubscribeFeed($feed, $callbackUrl = null)
{
$options = [
'body' => [
'hub.mode' => 'unsubscribe',
'hub.topic' => $feed,
]
];
if ($callbackUrl !== null) {
$options['body']['hub.callback'] = $callbackUrl;
}
return $this->post('/', $options);
}
/**
* @param string $callbackUrl
* @param int $page
* @return \GuzzleHttp\Message\ResponseInterface
*/
public function listFeeds($callbackUrl, $page = 1)
{
$options = [
'body' => [
'hub.mode' => 'list',
'hub.callback' => $callbackUrl,
'page' => $page
]
];
return $this->get('/', $options);
}
/**
* @param string $feed
* @param array $config Extra configs like: count, before, after, format, callback
* @return \GuzzleHttp\Message\ResponseInterface
*/
public function retrieveFeeds($feed, array $config = [])
{
$options = [
'body' => [
'hub.mode' => 'retrieve',
'hub.topic' => $feed,
]
];
array_merge($options['body'], $config);
return $this->get('/', $options);
}
/**
* Checks the call to see if it was signed by Superfeedr
* @param string $signature
* @param mixed $content
* @return bool
*/
public function validateRequest($signature, $content)
{
$sum = hash_hmac('sha1', $content, $this->hubSecret);
return ($signature === $sum);
}
}