PHP 8.0.0 Migration Guide

The PHP SDK improves compatibility and performance in v8.0.0. The legacy transport layer is replaced with an implementation that follows PHP Standards Recommendation (PSR). By default, the SDK uses the Guzzle HTTP client.

What has changed

See the major differences between versions:

FeaturePHP SDK v7.x.xPHP SDK v8.x.x
Method to set transport layer
PubNub::setTransport()
PubNub::setClient()
Transport was removed

PubNub::setTransport() is removed. Use PubNub::setClient() instead.

Required changes

If you didn't override the default transport, this update is seamless.

If you did, your client must implement PSR-18. Optionally, expose an extended send method that accepts request options for a single call.

1use Psr\Http\Client\ClientInterface;
2
3class CustomClient implements ClientInterface
4{
5 /**
6 * Sends a PSR-7 request and returns a PSR-7 response.
7 *
8 * @param RequestInterface $request
9 *
10 * @return ResponseInterface
11 *
12 * @throws \Psr\Http\Client\ClientExceptionInterface If an error happens while processing the request.
13 */
14 public function sendRequest(RequestInterface $request): ResponseInterface { }
15
show all 25 lines

Use default HTTP client

If you don't need a custom transport, use the default GuzzleHttp client.

The default setup works out of the box. It suits most use cases and provides a reliable, efficient HTTP client.

1use PubNub\PubNub;
2use PubNub\PNConfiguration;
3
4$config = new PNConfiguration();
5$config->setPublishKey('demo');
6$config->setSubscribeKey('demo');
7$config->setUserId('demo');
8$pubnub = new PubNub();
9$pubnub->publish()->channel('test')->message('Hello')->sync();

Use custom HTTP client

To use a custom HTTP client, implement PSR‑18 ClientInterface. The example below uses built‑in cURL.

1class CustomClient implements ClientInterface
2{
3 public function sendRequest(RequestInterface $request): ResponseInterface
4 {
5 $ch = curl_init();
6
7 curl_setopt($ch, CURLOPT_URL, (string) $request->getUri());
8 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request->getMethod());
9 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
10
11 $headers = [];
12 foreach ($request->getHeaders() as $name => $values) {
13 foreach ($values as $value) {
14 $headers[] = $name . ': ' . $value;
15 }
show all 34 lines

This class uses cURL to send the request. It returns a Response (an implementation of ResponseInterface) and uses a Stream that implements StreamInterface.

To use this custom client with the SDK, instantiate it and call PubNub::setClient.

1use PubNub\PubNub;
2use PubNub\PNConfiguration;
3use CustomClient;
4
5$config = new PNConfiguration();
6$config->setPublishKey("demo");
7$config->setSubscribeKey("demo");
8$config->setUserId("test");
9
10$client = new CustomClient();
11
12$pubnub = new PubNub($config);
13$pubnub->setClient($client);
14$pubnub->publish()->channel('test')->message('Hello')->sync();

Response

Basic Response implementation:

1use Psr\Http\Message\ResponseInterface;
2
3class Response implements ResponseInterface
4{
5 private int $statusCode;
6 private array $headers;
7 private StreamInterface $body;
8
9 public function __construct(int $statusCode, array $headers, string $body)
10 {
11 $this->statusCode = $statusCode;
12 $this->headers = $headers;
13 $this->body = new Stream($body);
14 }
15
show all 90 lines

Stream

Basic Stream implementation:

1use Psr\Http\Message\StreamInterface;
2
3class Stream implements StreamInterface
4{
5 private $stream;
6 private $size;
7
8 public function __construct(string $content = '')
9 {
10 $this->stream = fopen('php://temp', 'r+');
11 fwrite($this->stream, $content);
12 rewind($this->stream);
13 $this->size = strlen($content);
14 }
15
show all 98 lines
Last updated on