Start with PHP

Learn how to setup your first PHP project powered by Appwrite.

1

Create project

Head to the Appwrite Console.

If this is your first time using Appwrite, create an account and create your first project.

Create project screen

Create project screen

Then, under Integrate with your server, add an API Key with the following scopes.

Create project screen

Create project screen

CategoryRequired scopesPurpose
Databasedatabases.writeAllows API key to create, update, and delete databases.
collections.writeAllows API key to create, update, and delete collections.
attributes.writeAllows API key to create, update, and delete attributes.
documents.readAllows API key to read documents.
documents.writeAllows API key to create, update, and delete documents.

Other scopes are optional.

2

Create PHP project

Create a PHP CLI application.

Shell
mkdir my-app
cd my-app
composer init
3

Install Appwrite

Install the PHP Appwrite SDK.

Shell
composer require appwrite/appwrite:10.1.0
4

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.

Project settings screen

Project settings screen

Create a new file index.php and initialize the Appwrite Client. Replace <YOUR_PROJECT_ID> with your project ID and <YOUR_API_KEY> with your API key.

PHP
<?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('<YOUR_PROJECT_ID>')
    ->setKey('<YOUR_API_KEY>');
5

Initialize database

Once the Appwrite Client is initialized, create a function to configure a todo collection.

PHP
$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];
}
6

Add documents

Create a function to add some mock data into your new collection.

PHP
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
    );
}
7

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();.

PHP
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);
8

All set

Run your project with php src/index.php and view the response in your console.