Skip to main content

Getting Started with Momento Storage in PHP

If you need to get going quickly with PHP and Momento Storage, this page contains the basic API calls you'll need.

For more info, you can also see the PHP SDK on GitHub.

Prerequisites

Configure composer to get the PHP SDK

Add our repository to your composer.json file and our SDK as a dependency:

{
"require": {
"momentohq/client-sdk-php": "1.11.1"
}
}

Run composer update to install the necessary prerequisites.

Import libraries and connect to return a StorageClient object

This code pulls in the necessary imports and instantiates the StorageClient that will be used to interact with your store.

<?php
require "vendor/autoload.php";
use Momento\Auth\CredentialProvider;
use Momento\Config\Configurations\Storage\Laptop;
use Momento\Storage\PreviewStorageClient;

$storageClient = new PreviewStorageClient(
Laptop::latest(),
CredentialProvider::fromEnvironmentVariable("MOMENTO_API_KEY")
);

Create a new store in Momento Storage

Use this function to create a new store in your account.


$create_store_response = $storage_client->createStore($store_name);
if ($create_store_response->asSuccess()) {
print("Store $store_name created\n");
} elseif ($create_store_response->asAlreadyExists()) {
print("Store $store_name already exists\n");
} elseif ($err = $create_store_response->asError()) {
print("An error occurred while attempting to create $store_name: {$err->errorCode()} - {$err->message()}\n");
}

List the names of existing stores in your account

A simple list of the names of stores for the account.


$list_stores_response = $storage_client->listStores();
if ($listSuccess = $list_stores_response->asSuccess()) {
print("Found stores:\n");
foreach ($listSuccess->stores() as $store) {
$store_name = $store->name();
print("- $store_name\n");
}
} elseif ($err = $list_stores_response->asError()) {
print("An error occurred while attempting to list stores: {$err->errorCode()} - {$err->message()}\n");
}

Write an item to a store

A simple example of doing a put operation.


$put_response = $storage_client->putString($store_name, "test-key", "test-value");
if ($put_response->asSuccess()) {
print("Key 'test-key' stored successfully\n");
} elseif ($err = $put_response->asError()) {
print("An error occurred while attempting to store 'test-key': {$err->errorCode()} - {$err->message()}\n");
}

// Momento storage also supports int, float, and bytes types.
// Because strings in PHP are a series of bytes, the putBytes method accepts a string as the value.
$put_response = $storage_client->putBytes($store_name, "test-key", "test-value");
$put_response = $storage_client->putInt($store_name, "test-key", 42);
$put_response = $storage_client->putFloat($store_name, "test-key", 3.14);

Read an item from a store

This is an example of a simple read operation to get an item from a store.


$get_response = $storage_client->get($store_name, "test-key");
if ($found = $get_response->asFound()) {
$value_type = $found->type();
if ($value_type == StorageValueType::STRING) {
print("Got string value: " . $found->valueString() . "\n");
} elseif ($value_type == StorageValueType::INT) {
print("Got integer value: " . $found->valueInt() . "\n");
} elseif ($value_type == StorageValueType::FLOAT) {
print("Got float value: " . $found->valueFloat() . "\n");
} elseif ($value_type == StorageValueType::BYTES) {
// This case is not expected in this example as PHP doesn't have a native byte type
print("Got bytes value: " . $found->valueBytes() . "\n");
}
// You may also pull the value directly from the response without type checking
print("Retrieved value for key 'test-key': {$get_response->value()}\n");
} elseif ($get_response->asNotFound()) {
print("Key 'test-key' was not found in store $store_name\n");
} elseif ($err = $get_response->asError()) {
print("An error occurred while attempting to get key 'test-key' from store $store_name: {$err->errorCode()} - {$err->message()}\n");
}

Delete an item from a store

This is an example of a simple delete operation to remove an item from a store.


$delete_response = $storage_client->delete($store_name, "test-key");
if ($delete_response->asSuccess()) {
print("Key 'test-key' deleted successfully\n");
} elseif ($err = $delete_response->asError()) {
print("An error occurred while attempting to delete key 'test-key' from store $store_name: {$err->errorCode()} - {$err->message()}\n");
}

Running the code

You can find complete, working examples in the PHP SDK GitHub repo examples directory.

info

Beyond these basic API calls check out the API reference page for more information on the full assortment of Momento API calls.