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:
Feature | PHP SDK v7.x.x | PHP 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.
use Psr\Http\Client\ClientInterface;
class CustomClient implements ClientInterface
{
/**
* Sends a PSR-7 request and returns a PSR-7 response.
*
* @param RequestInterface $request
*
* @return ResponseInterface
*
* @throws \Psr\Http\Client\ClientExceptionInterface If an error happens while processing the request.
*/
public function sendRequest(RequestInterface $request): ResponseInterface { }
show all 25 linesUse 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.
use PubNub\PubNub;
use PubNub\PNConfiguration;
$config = new PNConfiguration();
$config->setPublishKey('demo');
$config->setSubscribeKey('demo');
$config->setUserId('demo');
$pubnub = new PubNub();
$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.
class CustomClient implements ClientInterface
{
public function sendRequest(RequestInterface $request): ResponseInterface
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, (string) $request->getUri());
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request->getMethod());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = [];
foreach ($request->getHeaders() as $name => $values) {
foreach ($values as $value) {
$headers[] = $name . ': ' . $value;
}
show all 34 linesThis 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
.
use PubNub\PubNub;
use PubNub\PNConfiguration;
use CustomClient;
$config = new PNConfiguration();
$config->setPublishKey("demo");
$config->setSubscribeKey("demo");
$config->setUserId("test");
$client = new CustomClient();
$pubnub = new PubNub($config);
$pubnub->setClient($client);
$pubnub->publish()->channel('test')->message('Hello')->sync();
Response
Basic Response
implementation:
use Psr\Http\Message\ResponseInterface;
class Response implements ResponseInterface
{
private int $statusCode;
private array $headers;
private StreamInterface $body;
public function __construct(int $statusCode, array $headers, string $body)
{
$this->statusCode = $statusCode;
$this->headers = $headers;
$this->body = new Stream($body);
}
show all 90 linesStream
Basic Stream
implementation:
use Psr\Http\Message\StreamInterface;
class Stream implements StreamInterface
{
private $stream;
private $size;
public function __construct(string $content = '')
{
$this->stream = fopen('php://temp', 'r+');
fwrite($this->stream, $content);
rewind($this->stream);
$this->size = strlen($content);
}
show all 98 lines