Skip to content

Commit cb8453b

Browse files
committed
added example to use custom client implementation
1 parent 80638ee commit cb8453b

File tree

1 file changed

+244
-0
lines changed

1 file changed

+244
-0
lines changed

examples/customClient.php

+244
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
<?php
2+
3+
require_once 'vendor/autoload.php';
4+
5+
use PubNub\PubNub;
6+
use PubNub\PNConfiguration;
7+
use Psr\Http\Message\RequestInterface;
8+
use Psr\Http\Message\ResponseInterface;
9+
use Psr\Http\Client\ClientInterface;
10+
use Psr\Http\Message\StreamInterface;
11+
12+
class Response implements ResponseInterface
13+
{
14+
private int $statusCode;
15+
private array $headers;
16+
private StreamInterface $body;
17+
18+
public function __construct(int $statusCode, array $headers, string $body)
19+
{
20+
$this->statusCode = $statusCode;
21+
$this->headers = $headers;
22+
$this->body = new Stream($body);
23+
}
24+
25+
public function getStatusCode(): int
26+
{
27+
return $this->statusCode;
28+
}
29+
30+
public function getReasonPhrase(): string
31+
{
32+
return '';
33+
}
34+
35+
public function getProtocolVersion(): string
36+
{
37+
return '';
38+
}
39+
40+
public function getHeaders(): array
41+
{
42+
return $this->headers;
43+
}
44+
45+
public function getBody(): StreamInterface
46+
{
47+
return $this->body;
48+
}
49+
50+
public function withStatus($code, $reasonPhrase = ''): self
51+
{
52+
$this->statusCode = $code;
53+
return $this;
54+
}
55+
56+
public function withHeader($name, $value): self
57+
{
58+
$this->headers[$name] = $value;
59+
return $this;
60+
}
61+
62+
public function withAddedHeader($name, $value): self
63+
{
64+
$this->headers[$name][] = $value;
65+
return $this;
66+
}
67+
68+
public function withoutHeader($name): self
69+
{
70+
unset($this->headers[$name]);
71+
return $this;
72+
}
73+
74+
public function withProtocolVersion($version): self
75+
{
76+
return $this;
77+
}
78+
79+
public function hasHeader($name): bool
80+
{
81+
return isset($this->headers[$name]);
82+
}
83+
84+
public function getHeader($name): array
85+
{
86+
return $this->headers[$name] ?? [];
87+
}
88+
89+
public function getHeaderLine($name): string
90+
{
91+
return implode(', ', $this->headers[$name] ?? []);
92+
}
93+
94+
public function withBody(StreamInterface $body): self
95+
{
96+
$this->body = $body;
97+
return $this;
98+
}
99+
}
100+
101+
class Stream implements StreamInterface
102+
{
103+
private $stream;
104+
private $size;
105+
106+
public function __construct(string $content = '')
107+
{
108+
$this->stream = fopen('php://temp', 'r+');
109+
fwrite($this->stream, $content);
110+
rewind($this->stream);
111+
$this->size = strlen($content);
112+
}
113+
114+
public function __toString(): string
115+
{
116+
return stream_get_contents($this->stream, -1, 0);
117+
}
118+
119+
public function close(): void
120+
{
121+
fclose($this->stream);
122+
}
123+
124+
public function detach()
125+
{
126+
$result = $this->stream;
127+
$this->stream = null;
128+
return $result;
129+
}
130+
131+
public function getSize(): ?int
132+
{
133+
return $this->size;
134+
}
135+
136+
public function tell(): int
137+
{
138+
return ftell($this->stream);
139+
}
140+
141+
public function eof(): bool
142+
{
143+
return feof($this->stream);
144+
}
145+
146+
public function isSeekable(): bool
147+
{
148+
return true;
149+
}
150+
151+
public function seek($offset, $whence = SEEK_SET): void
152+
{
153+
fseek($this->stream, $offset, $whence);
154+
}
155+
156+
public function rewind(): void
157+
{
158+
rewind($this->stream);
159+
}
160+
161+
public function isWritable(): bool
162+
{
163+
return true;
164+
}
165+
166+
public function write($string): int
167+
{
168+
$result = fwrite($this->stream, $string);
169+
$this->size += strlen($string);
170+
return $result;
171+
}
172+
173+
public function isReadable(): bool
174+
{
175+
return true;
176+
}
177+
178+
public function read($length): string
179+
{
180+
return fread($this->stream, $length);
181+
}
182+
183+
public function getContents(): string
184+
{
185+
return stream_get_contents($this->stream);
186+
}
187+
188+
public function getMetadata($key = null)
189+
{
190+
$meta = stream_get_meta_data($this->stream);
191+
if ($key === null) {
192+
return $meta;
193+
}
194+
return $meta[$key] ?? null;
195+
}
196+
}
197+
198+
class CustomClient implements ClientInterface
199+
{
200+
public function sendRequest(RequestInterface $request): ResponseInterface
201+
{
202+
$ch = curl_init();
203+
204+
curl_setopt($ch, CURLOPT_URL, (string) $request->getUri());
205+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request->getMethod());
206+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
207+
208+
$headers = [];
209+
foreach ($request->getHeaders() as $name => $values) {
210+
foreach ($values as $value) {
211+
$headers[] = $name . ': ' . $value;
212+
}
213+
}
214+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
215+
216+
if ($request->getBody()->getSize() > 0) {
217+
curl_setopt($ch, CURLOPT_POSTFIELDS, (string) $request->getBody());
218+
}
219+
print("\n doing request...");
220+
$responseBody = curl_exec($ch);
221+
print("done\n\n");
222+
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
223+
224+
if ($responseBody === false) {
225+
throw new \RuntimeException('Curl error: ' . curl_error($ch));
226+
}
227+
228+
curl_close($ch);
229+
230+
return new Response($responseCode, [], $responseBody);
231+
}
232+
}
233+
234+
$config = new PNConfiguration();
235+
$config->setPublishKey('demo');
236+
$config->setSubscribeKey('demo');
237+
$config->setUuid('example');
238+
239+
$pubnub = new PubNub($config);
240+
$client = new CustomClient();
241+
$pubnub->setClient($client);
242+
243+
$time = $pubnub->time()->sync();
244+
print_r($time->getTimetoken());

0 commit comments

Comments
 (0)