Using Redis with PHP: Installation and Examples
Installation
Before starting to use Redis in PHP, we need to ensure that the Redis service and the PHP Redis driver are installed, and that PHP is functioning correctly on your machine. Next, let's install the PHP Redis driver: the download link is https://github.com/phpredis/phpredis/releases.
Install Redis Extension for PHP
The following operations need to be completed in the downloaded phpredis directory:
$ wget https://github.com/phpredis/phpredis/archive/2.2.4.tar.gz
$ cd phpredis-2.2.7 # Enter the phpredis directory
$ /usr/local/php/bin/phpize # Path after PHP installation
$ ./configure --with-php-config=/usr/local/php/bin/php-config
$ make && make install
If you are using PHP 7, you need to download the specified branch:
git clone -b php7 https://github.com/phpredis/phpredis.git
Modify php.ini File
vi /usr/local/php/lib/php.ini
Add the following content:
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20090626"
extension=redis.so
After installation, restart php-fpm or Apache. Check the phpinfo information, and you will see the Redis extension.

Connect to Redis Service
<?php
// Connect to local Redis service
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server sucessfully";
// Check if the service is running
echo "Server is running: " . $redis->ping();
?>
Executing the script produces the following output:
Connection to server sucessfully
Server is running: PONG
Redis PHP String Example
<?php
// Connect to local Redis service
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server sucessfully";
// Set Redis string data
$redis->set("tutorial-name", "Redis tutorial");
// Retrieve stored data and output
echo "Stored string in redis:: " . $redis->get("tutorial-name");
?>
Executing the script produces the following output:
Connection to server sucessfully
Stored string in redis:: Redis tutorial
Redis PHP List Example
<?php
// Connect to local Redis service
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server sucessfully";
// Store data in the list
$redis->lpush("tutorial-list", "Redis");
$redis->lpush("tutorial-list", "Mongodb");
$redis->lpush("tutorial-list", "Mysql");
// Retrieve stored data and output
$arList = $redis->lrange("tutorial-list", 0, 5);
echo "Stored string in redis";
print_r($arList);
?>
Executing the script produces the following output:
Connection to server sucessfully
Stored string in redis
Redis
Mongodb
Mysql
Redis PHP Keys Example
<?php
// Connect to local Redis service
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server sucessfully";
// Retrieve data and output
$arList = $redis->keys("*");
echo "Stored keys in redis:: ";
print_r($arList);
?>
Executing the script produces the following output:
Connection to server sucessfully
Stored string in redis::
tutorial-name
tutorial-list
Original source: PHP 使用 Redis. All rights reserved by the original author. If there is any infringement, please contact QQ: 337003006 for deletion.