Skip to main content

Connect with PHP

This example connects to Redis®* service from PHP, making use of the predis library.


These are the placeholders you will need to replace in the code sample:

REDIS_URIURL for the Redis connection, from the service overview page


Install the predis library:

composer require predis/predis


Create a new file named index.php, add the following content and replace the placeholder with the Redis URI:


require 'vendor/autoload.php';

$redis_uri = 'REDIS_URI';

$client = new Predis\Client($redis_uri);

$client->set('key', 'hello world');
$value = $client->get('key');

echo "The value of key is: {$value}";

This code creates a key named key with the value hello world and no expiration time. Then, it gets the key back from Redis and prints its value.

Run the code:

php index.php

If the script runs successfully, the outputs should be:

The value of key is: hello world