PHP Connection Example

Last updated: 2021-02-25 15:15:03

Preparations before running:

Download the phpredis client.

Sample code:

<?php
  /**For the following parameters, enter your Redis instance's private IP, port number, instance ID and password*/
  $host = "192.xx.xx.2";
  $port = 6379;
  $instanceid = "c532952f-55dc-4c22-a941-63057e560788";
  $pwd = "123tj6na";

  $redis = new Redis();
  // Connect to Redis
  if ($redis->connect($host, $port) == false) {
    die($redis->getLastError());
  }
  // Authenticate
   if ($redis->auth($pwd) == false) {
    die($redis->getLastError());
  }

  /**You can start manipulating the Redis instance. For more information, see https://github.com/phpredis/phpredis */

  // Set the Key
  if ($redis->set("redis", "tencent") == false) {
    die($redis->getLastError());
  }
  echo "set key redis suc, value is:tencent\n";

  // Get the Key
  $value = $redis->get("redis");
  echo "get key redis is:".$value."\n";
?>

Execution result: