PHP API & SDK Docs 9.0.3
This guide walks you through a simple "Hello, World" application that demonstrates the core concepts of PubNub:
- Setting up a connection
- Sending messages
- Receiving messages in real-time
Overview
This guide will help you get up and running with PubNub in your PHP application. We'll create a simple application that demonstrates how to connect to PubNub, subscribe to channels, and publish messages.
The code examples will work in both web environments and command-line scripts, with notes indicating any environment-specific considerations when relevant.
Prerequisites
Before we dive in, make sure you have:
- A basic understanding of PHP (v7.0 or newer recommended)
- PHP installed on your system (either on a web server or as CLI)
- Composer (recommended) or the ability to manually manage dependencies
- A PubNub account (we'll help you set this up!)
Setup
Get your PubNub keys
First, get your PubNub keys:
- Sign in or create an account on the PubNub Admin Portal.
- Create an app (or use an existing one).
- Find your publish and subscribe keys in the app dashboard.
When you create an app, PubNub automatically generates a keyset. You can use the same keyset for development and production, but we recommend separate keysets for each environment to improve security and management.
Install the SDK
SDK version
Always use the latest SDK version to have access to the newest features and avoid security vulnerabilities, bugs, and performance issues.
Install the PHP SDK using Composer.
-
For a fresh project (no existing code), run the following command in the project directory.
1composer init -
Then, run this command (for both the existing Composer projects and the newly initiated ones).
1composer require pubnub/pubnub
Steps
Initialize PubNub
Create a PHP file to initialize the PubNub client: