Learn how to setup your first PHP project powered by Appwrite.
Create project
Head to the Appwrite Console.
If this is your first time using Appwrite, create an account and create your first project.
Then, under Integrate with your server, add an API Key with the following scopes.
Category | Required scopes | Purpose |
Database | databases.write | Allows API key to create, update, and delete databases. |
collections.write | Allows API key to create, update, and delete collections. | |
attributes.write | Allows API key to create, update, and delete attributes. | |
documents.read | Allows API key to read documents. | |
documents.write | Allows API key to create, update, and delete documents. |
Other scopes are optional.
Create PHP project
Create a PHP CLI application.
mkdir my-app
cd my-app
composer init
Install Appwrite
Install the PHP Appwrite SDK.
composer require appwrite/appwrite:11.0.1
Import Appwrite
Find your project ID in the Settings page. Also, click on the View API Keys button to find the API key that was created earlier.
Create a new file index.php and initialize the Appwrite Client. Replace <PROJECT_ID> with your project ID and <YOUR_API_KEY> with your API key.
<?php
require_once 'vendor/autoload.php';
use Appwrite\Client;
use Appwrite\Services\Databases;
use Appwrite\ID;
$client = new Client();
$client
->setEndpoint('https://cloud.appwrite.io/v1')
->setProject('<PROJECT_ID>')
->setKey('<YOUR_API_KEY>');
Initialize database
Once the Appwrite Client is initialized, create a function to configure a todo collection.
$databases = new Databases($client);
function prepareDatabase($databases) {
$todoDatabase = $databases->create(
databaseId: ID::unique(),
name: 'TodosDB'
);
$todoCollection = $databases->createCollection(
databaseId: $todoDatabase['$id'],
collectionId: ID::unique(),
name: 'Todos'
);
$databases->createStringAttribute(
databaseId: $todoDatabase['$id'],
collectionId: $todoCollection['$id'],
key: 'title',
size: 255,
required: true
);
$databases->createStringAttribute(
databaseId: $todoDatabase['$id'],
collectionId: $todoCollection['$id'],
key: 'description',
size: 255,
required: false,
);
$databases->createBooleanAttribute(
databaseId: $todoDatabase['$id'],
collectionId: $todoCollection['$id'],
key: 'isComplete',
required: true
);
return [$todoDatabase, $todoCollection];
}
Add documents
Create a function to add some mock data into your new collection.
function seedDatabase($databases, $todoDatabase, $todoCollection) {
$testTodo1 = [
'title' => 'Buy apples',
'description' => 'At least 2KGs',
'isComplete' => true
];
$testTodo2 = [
'title' => 'Wash the apples',
'isComplete' => true
];
$testTodo3 = [
'title' => 'Cut the apples',
'description' => 'Don\'t forget to pack them in a box',
'isComplete' => false
];
$databases->createDocument(
$todoDatabase['$id'],
$todoCollection['$id'],
ID::unique(),
$testTodo1
);
$databases->createDocument(
$todoDatabase['$id'],
$todoCollection['$id'],
ID::unique(),
$testTodo2
);
$databases->createDocument(
$todoDatabase['$id'],
$todoCollection['$id'],
ID::unique(),
$testTodo3
);
}
Retrieve documents
Create a function to retrieve the mock todo data and a function to execute the requests in order. Run the functions to by calling runAllTasks();.
function getTodos($databases, $todoDatabase, $todoCollection) {
$todos = $databases->listDocuments(
$todoDatabase['$id'],
$todoCollection['$id']
);
foreach ($todos['documents'] as $todo) {
echo "Title: {$todo['title']}\n" .
"Description: {$todo['description']}\n" .
"Is Todo Complete: {$todo['isComplete']}\n\n";
}
}
function runAllTasks($databases) {
[$todoDatabase, $todoCollection] = prepareDatabase($databases);
seedDatabase($databases, $todoDatabase, $todoCollection);
getTodos($databases, $todoDatabase, $todoCollection);
}
runAllTasks($databases);
All set
Run your project with php src/index.php and view the response in your console.