Connect using PHP for Public Cloud Databases for Valkey
Connect to your Public Cloud Databases for Valkey using the PHP programming language
Objective
Public Cloud Databases allow you to focus on building and deploying cloud applications while OVHcloud takes care of the database infrastructure and maintenance in operational conditions.
This guide explains how to connect to a Valkey service instance with one of the world's most famous programming language: PHP.
Requirements
- A Public Cloud project in your OVHcloud account
- A Valkey service running on your OVHcloud Public Cloud Databases (this guide can help you to meet this requirement)
- Configure your Valkey service to accept incoming connections
- A PHP environment with a stable version and public network connectivity (Internet). This guide was made using PHP 8.4.
Concept
A Valkey service can be managed through multiple ways. One of the easiest, yet powerful, is to use a Command Line Interface (CLI), as shown in our guide: Connect with CLI.
Another way is to interact directly using programming languages, such as PHP. Valkey has multiple PHP clients to connect to and manage an instance from code. See the official list of Valkey clients for the latest information.
We will need to set up our PHP environment with the phpredis client, then configure our Public Cloud Databases for Valkey services to accept incoming connections, and finally code in PHP to perform a few example actions.
Instructions
Set up your PHP environment
To interact with your instance with PHP, your development environment needs to be configured with:
- A compatible version of PHP;
- A PHP client that supports Valkey and TLS, for example:
- the phpredis extension (version 6.3.0 at the time of writing), which supports Valkey, installed with
pecl install redis; - the Predis library (version 3.6.1 at the time of writing), a Redis/Valkey client written in PHP, installed with
composer require predis/predis.
- the phpredis extension (version 6.3.0 at the time of writing), which supports Valkey, installed with
For both phpredis and Predis it is recommended to check their respective sites on how to do the install:
We are now ready to learn how to connect to our instance. We will use phpredis as the PHP client.
Connect with PHP
Get the host and the port of your service in the Dashboard tab, Connection information section, as explained in the Configure incoming connections guide.
TLS is mandatory: connections without TLS are refused. With phpredis, the tls:// prefix in front of the host enables TLS. The server certificate is signed by a public certification authority: PHP verifies it by default (SSL context options verify_peer and verify_peer_name) with the CA certificates of your system, so you do not need any CA file. Do not disable this verification.
In your PHP environment, let's try a connection. To be sure that we are indeed connected, set a data pair and then check we can get the value of it. Replace <username>, <password>, <host> and <port> with your own values:
Congratulations! Everything is working properly.
Avoid writing your password in your source code: read it from an environment variable (for example getenv('VALKEY_PASSWORD')) or from a secret manager.
If you get a NOPERM error, the permissions of your user do not allow the key or the command used. In our example, the user must be allowed to read and write keys matching cache:*. Review its keys, categories and commands in the Users tab, as explained in the Configure incoming connections guide.
Go further
Visit our dedicated Discord channel: https://discord.gg/ovhcloud. Ask questions, provide feedback and interact directly with the team that builds our databases services.
For training or technical assistance implementing our solutions, contact your sales representative or visit our Professional Services page to request a quote and have your project analysed by our experts.
Join our community of users.