To use Redis with PHP, you need a PHP Redis client.

Here, we show Predis, a flexible and feature-complete Redis client library for PHP version 5.3 and later.

Other Redis clients are available for PHP; see the PHP section of the Redis Clients page.

Install Predis

See the How to install and use Predis section of the Predis client’s README file for installation instructions.

The recommended method to install Predis is to use Composer and install it from Packagist or the dedicated PEAR channel.

You can also download the latest Predis release from the GitHub repository.

Connect to Redis

The following code creates a connection to Redis using Predis:

<?php
require "predis/autoload.php";
Predis\Autoloader::register();
    
$redis = new Predis\Client(array(
    "scheme" => "tcp",
    "host" => "hostname",
    "port" => port,
    "password" => "password"));
echo "Connected to Redis";
?>
Note:
If you aren’t autoloading PHP dependencies, use require to load Predis and then call its register method, as shown here. To learn more, see Loading the library.

To adapt this example to your code, replace the following values with your database’s values:

  • In line 8, set host to your database’s hostname or IP address
  • In line 9, set port to your database’s port
  • In line 10, set password to your database’s password

Example code for Redis commands

Once connected to Redis, you can read and write data. The following code snippet assigns the value bar to the Redis key foo, reads it back, and prints it:

// open a connection to Redis
...
 
$redis->set("foo", "bar");
$value = $redis->get("foo");
var_dump($value);

Example output:

$ php predis_example.php
Connected to Redis
string(3) "bar"

Persistent connections

Predis supports the use of persistent connections, which are recommended to minimize connection management overhead.

To enable persistent connections, use the persistent connection attribute, as shown in the following code snippet:

$redis = new Predis\Client(array(
    "scheme" => "tcp",
    "host" => "hostname",
    "port" => port,
    "password" => "password",
    "persistent" => "1"));

Encrypted connections

As of v1.1.0, you can encrypt connections by specifying tls or rediss as the value of the scheme attribute or as part of the URI.

In addition, you can configure connection parameters, as shown here:

// Named array of connection parameters:
$client = new Predis\Client([
  'scheme' => 'tls',
  'ssl'    => ['cafile' => 'private.pem', 'verify_peer' => true],
]);

// Same set of parameters, but using an URI string:
$client = new Predis\Client('tls://127.0.0.1?ssl[cafile]=private.pem&ssl[verify_peer]=1');

To learn more, see Connecting to Redis.

When using earlier versions of Predis (which is not recommended), you can use stunnel or this Predis fork that has been added with SSL support.