How to connect PHP to a Redis instance on ObjectRocket

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

PHP, standing for hypertext preprocessor, is an open source scripting language that can easily be embedded into HTML, making it particularly suitable for web development application. Redis, an abbreviation for remote dictionary server, is considered one of the go-to software programs for structured data storing. The Redis program is an open-source key-value store, can manage a wide variety of high-level data types data, such as sets and lists, among others, and has the unique quality of being readily compatible with PHP. This tutorial will explain how to connect PHP to a Redis instance on ObjectRocket.

Prerequisites

  • XAMPP must be properly installed and configured on the local machine.

  • Composer must also be properly installed on the system.

  • The Redis tools must also be properly installed and configured on the same device.

  • An instance of Redis must be created for an ObjectRocket account in the Mission Control panel. An image of the panel is shown here:

Screenshot

  • Stunnel or the same open-source proxy for client and remote server connection, must also be installed and configured.

NOTE: In this tutorial, the connection will be routed to localhost and port 8000.

Connecting PHP to a Redis Instance on ObjectRocket

This section will explain how to create a PHP project structure and connect to a PHP application.

Create PHP Project Structure

The following steps will create a simple project structure of a basic PHP application that will connect to a Redis instance:

Step 1) Create a folder named phpredis inside the htdocs folder under the XAMPP directory.

Step 2) Create a new file named composer.json, that will handling the necessary dependencies, if any. Append the following script in the composer.json file:

1
2
3
4
5
6
7
{
    "autoload": {
        "psr-4": {
            "phpredis\": "app/"
        }
    }
}

Step 3) Navigate to the project directory, via the terminal, and execute the following command:

1
2
3
4
5
C:\xampp\htdocs\phpredis- composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files

Here the update command will update the dependencies that are specified inside the composer.json file. However, since none were specified, the system will generate the following message:

1
2
Nothing to install or update
Generating load files.

Step 4) Create a PHP file named phpredis.php that will host the connection scripts to the Redis instance.

When done correctly, the completed project structure should resemble the following image:

Screenshot

Coding the PHP Application

The following demonstrates how to create the PHP script and how to connect PHP to a Redis instance on ObjectRocket:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
   //Connecting to Redis server on localhost
   $redis = new Redis();
   // We provide the connection setting of the stunnel4 configuration
   // remember that the stunnel4 binded the connection details of Redis instance and route the connection to the localhost and listens to port 8000
   $redis->connect('localhost', 8000);

   // The password is located at the Redis instance "CONNECT" tab.
   // To show the password, just tick the "eye" icon
   $redis->auth('_your_redis_instance_password');
   echo "Connection to server sucessfully ";
   echo "<br>";

   //this checks whether server is running or not
   echo "Server is running: ".$redis->ping();
?>

Testing the Application

All that is left to do now is test the application. Launch the XAMPP control panel and start the Apache module.

Now navigate to the application, via the browser, using the following URL: http://localhost/phpredis/phpredis.php.

The results should resemble the following image:

Screenshot

Conclusion

This tutorial covered how to connect PHP to a Redis instance on ObjectRocket and how to connect to a PHP application. The article explained the steps required to create a simple project structure of a basic PHP application needed to connect to a Redis instance. The tutorial then covered the coding for the PHP application. It then provided a demonstration of how to create the PHP script, how to connect PHP to a Redis instance on ObjectRocket and then lastly covered how to test the application. As several are needed, it is crucial to make certain that all of the prerequisites required to connect PHP to a Redis instance on ObjectRocket have been met before attempting to execute the examples provided in this tutorial.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.