{
  "schema_version": 2,
  "id": "develop/clients/php/connect",
  "title": "Connect to the server",
  "url": "https://redis.io/docs/latest/develop/clients/php/connect/",
  "summary": "Connect your PHP application to a Redis database",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-10-12T14:48:04+01:00",
  "page_type": "content",
  "content_hash": "3f87587c2795724b5604df85d58764ba70579e74cb1d2faf11bc8c3f7d6dcec5",
  "sections": [
    {
      "id": "basic-connection",
      "title": "Basic connection",
      "role": "overview",
      "text": "Connect to a locally-running server on the standard port (6379)\nwith the following code:\n\n[code example]\n\n> [!NOTE]\n> Predis supports [connection URLs](https://redis.io/docs/latest/develop/clients/connection-urls#predis-php) through the `PredisClient` constructor.\n> A URL can set the host, port, credentials, database, and whether to use TLS,\n> so you can use one instead of passing those options separately.\n\nStore and retrieve a simple string to test the connection:\n\n[code example]\n\nStore and retrieve a [hash](https://redis.io/docs/latest/develop/data-types/hashes)\nobject:\n\n[code example]"
    },
    {
      "id": "connect-to-a-redis-cluster",
      "title": "Connect to a Redis cluster",
      "role": "content",
      "text": "To connect to a Redis cluster, specify one or more of the nodes in\nthe `clusterNodes` parameter and set `'cluster'=>'redis'` in\n`options`:\n\n[code example]"
    },
    {
      "id": "connect-to-redis-sentinel",
      "title": "Connect to Redis Sentinel",
      "role": "content",
      "text": "To connect through [Redis Sentinel](https://redis.io/docs/latest/operate/oss_and_stack/management/sentinel),\npass a list of Sentinels as the first parameter. In `options`, set\n`'replication' => 'sentinel'` and set `service` to the name of the primary that the\nSentinels monitor:\n\n[code example]\n\nThe default timeout for connecting to a Sentinel is 100 milliseconds. If your Sentinels\nare on remote hosts, add a longer timeout to each Sentinel address, such as\n`tcp://<host>:26379?timeout=0.5`.\n\nThe Sentinels and the data nodes have separate credentials. Add the Sentinel credentials\nto each Sentinel address, and set the credentials for the primary and replicas in the\n`parameters` option. Include the Sentinel `username` even if it's `default`, because\notherwise Predis uses the `username` from `parameters` for the Sentinels too:\n\n[code example]\n\nPredis sends read-only commands to a replica until the first write command. After that, it\nsends all commands to the primary. Call `switchToSlave()` to send reads to a replica again.\nReplication is asynchronous, so a replica can return stale data.\n\n[code example]\n\nPredis doesn't listen for failover announcements from the Sentinels. After a failover, the\nclient keeps using the old primary until Sentinel reconfigures it as a replica and closes\nthe connection. Any writes that the old primary accepts during that time are lost."
    },
    {
      "id": "connect-to-your-production-redis-with-tls",
      "title": "Connect to your production Redis with TLS",
      "role": "content",
      "text": "When you deploy your application, use TLS and follow the\n[Redis security](https://redis.io/docs/latest/operate/oss_and_stack/management/security)\nguidelines.\n\nUse the following commands to generate the client certificate and private key:\n\n[code example]\n\nIf you have the [Redis source folder](https://github.com/redis/redis) available,\nyou can also generate the certificate and private key with these commands:\n\n[code example]\n\nPass this information during connection using the `ssl` section of `options`:\n\n[code example]"
    }
  ],
  "examples": [
    {
      "id": "basic-connection-ex0",
      "language": "php",
      "code": "<?php\n\nrequire 'vendor/autoload.php';\n\nuse Predis\\Client as PredisClient;\n\n$r = new PredisClient([\n                'scheme'   => 'tcp',\n                'host'     => '127.0.0.1',\n                'port'     => 6379,\n                'password' => '',\n                'database' => 0,\n            ]);",
      "section_id": "basic-connection"
    },
    {
      "id": "basic-connection-ex1",
      "language": "php",
      "code": "echo $r->set('foo', 'bar'), PHP_EOL;\n// >>> OK\n\necho $r->get('foo'), PHP_EOL;\n// >>> bar",
      "section_id": "basic-connection"
    },
    {
      "id": "basic-connection-ex2",
      "language": "php",
      "code": "$r->hset('user-session:123', 'name', 'John');\n$r->hset('user-session:123', 'surname', 'Smith');\n$r->hset('user-session:123', 'company', 'Redis');\n$r->hset('user-session:123', 'age', 29);\n\necho var_export($r->hgetall('user-session:123')), PHP_EOL;\n/* >>>\narray (\n  'name' => 'John',\n  'surname' => 'Smith',\n  'company' => 'Redis',\n  'age' => '29',\n)\n*/",
      "section_id": "basic-connection"
    },
    {
      "id": "connect-to-a-redis-cluster-ex0",
      "language": "php",
      "code": "$clusterNodes = [\n    'tcp://127.0.0.1:30001', // Node 1\n    'tcp://127.0.0.1:30002', // Node 2\n    'tcp://127.0.0.1:30003', // Node 3\n];\n$options    = ['cluster' => 'redis'];\n\n// Create a Predis client for the cluster\n$rc = new PredisClient($clusterNodes, $options);\n\necho $rc->cluster('nodes'), PHP_EOL;\n/* >>>\nd8773e888e92d015b7c52fc66798fd6815afefec 127.0.0.1:30004@40004 slave cde97d1f7dce13e9253ace5cafd3fb0aa67cda63 0 1730713764217 1 connected\n58fe1346de4c425d60db24e9b153926fbde0d174 127.0.0.1:30002@40002 master - 0 1730713763361 2 connected 5461-10922\n015ecc8148a05377dda22f19921d16efcdd6d678 127.0.0.1:30006@40006 slave c019b75d8b52e83e7e52724eccc716ac553f71d6 0 1730713764218 3 connected\naca365963a72642e6ae0c9503aabf3be5c260806 127.0.0.1:30005@40005 slave 58fe1346de4c425d60db24e9b153926fbde0d174 0 1730713763363 2 connected\nc019b75d8b52e83e7e52724eccc716ac553f71d6 127.0.0.1:30003@40003 myself,master - 0 1730713764000 3 connected 10923-16383\ncde97d1f7dce13e9253ace5cafd3fb0aa67cda63 127.0.0.1:30001@40001 master - 0 1730713764113 1 connected 0-5460\n*/\n\necho $rc->set('foo', 'bar'), PHP_EOL;\n// >>> OK\necho $rc->get('foo'), PHP_EOL;\n// >>> bar",
      "section_id": "connect-to-a-redis-cluster"
    },
    {
      "id": "connect-to-redis-sentinel-ex0",
      "language": "php",
      "code": "$sentinels = [\n    'tcp://localhost:26379',\n    'tcp://localhost:26380',\n    'tcp://localhost:26381',\n];\n$options = [\n    'replication' => 'sentinel',\n    'service'     => 'mymaster',\n];\n\n$r = new PredisClient($sentinels, $options);\n\necho $r->set('foo', 'bar'), PHP_EOL;\n// >>> OK\n\necho $r->get('foo'), PHP_EOL;\n// >>> bar",
      "section_id": "connect-to-redis-sentinel"
    },
    {
      "id": "connect-to-redis-sentinel-ex1",
      "language": "php",
      "code": "// Credentials for the Sentinels.\n$sentinels = [\n    'tcp://localhost:26379?username=yourSentinelUsername&password=yourSentinelPassword',\n    'tcp://localhost:26380?username=yourSentinelUsername&password=yourSentinelPassword',\n    'tcp://localhost:26381?username=yourSentinelUsername&password=yourSentinelPassword',\n];\n$options = [\n    'replication' => 'sentinel',\n    'service'     => 'mymaster',\n    // Credentials for the primary and replicas.\n    'parameters'  => [\n        'username' => 'yourUsername',\n        'password' => 'yourPassword',\n    ],\n];\n\n$r = new PredisClient($sentinels, $options);",
      "section_id": "connect-to-redis-sentinel"
    },
    {
      "id": "connect-to-redis-sentinel-ex2",
      "language": "php",
      "code": "// A write command switches the client to the primary.\necho $r->set('foo', 'baz'), PHP_EOL;\n// >>> OK\n\n// Switch back to a replica for later reads.\n$r->getConnection()->switchToSlave();\necho $r->get('foo'), PHP_EOL;\n// >>> baz",
      "section_id": "connect-to-redis-sentinel"
    },
    {
      "id": "connect-to-your-production-redis-with-tls-ex0",
      "language": "bash",
      "code": "openssl genrsa -out redis_user_private.key 2048\nopenssl req -new -key redis_user_private.key -out redis_user.csr\nopenssl x509 -req -days 365 -in redis_user.csr -signkey redis_user_private.key -out redis_user.crt",
      "section_id": "connect-to-your-production-redis-with-tls"
    },
    {
      "id": "connect-to-your-production-redis-with-tls-ex1",
      "language": "bash",
      "code": "./utils/gen-test-certs.sh\n./src/redis-server --tls-port 6380 --port 0 --tls-cert-file ./tests/tls/redis.crt --tls-key-file ./tests/tls/redis.key --tls-ca-cert-file ./tests/tls/ca.crt",
      "section_id": "connect-to-your-production-redis-with-tls"
    },
    {
      "id": "connect-to-your-production-redis-with-tls-ex2",
      "language": "php",
      "code": "$options = [\n    'scheme' => 'tls', // Use 'tls' for SSL connections\n    'host' => '127.0.0.1', // Redis server hostname\n    'port' => 6379, // Redis server port\n    'username' => 'default', // Redis username\n    'password' => '', // Redis password\n    'options' => [\n        'ssl' => [\n            'verify_peer' => true, // Verify the server's SSL certificate\n            'cafile' => './redis_ca.pem', // Path to CA certificate\n            'local_cert' => './redis_user.crt', // Path to client certificate\n            'local_pk' => './redis_user_private.key', // Path to client private key\n        ],\n    ],\n];\n\n$tlsConnection = new PredisClient($options);\n\necho $tlsConnection->set('foo', 'bar'), PHP_EOL;\n// >>> OK\necho $tlsConnection->get('foo'), PHP_EOL;\n// >>> bar",
      "section_id": "connect-to-your-production-redis-with-tls"
    }
  ]
}
