Files
website/src/routes/docs/quick-starts/php/+page.markdoc
Vincent (Wen Yu) Ge e9018e27c8 bump SDK versions
2024-04-26 22:10:53 -04:00

217 lines
6.1 KiB
Plaintext

---
layout: article
title: Start with PHP
description: Dive into our step-by-step guide on integrating Appwrite with your PHP server backend application. Get your backend up and running quickly with this tutorial.
difficulty: beginner
readtime: 5
back: /docs/quick-starts
---
Learn how to setup your first PHP project powered by Appwrite.
{% section #step-1 step=1 title="Create project" %}
Head to the [Appwrite Console](https://cloud.appwrite.io/console).
If this is your first time using Appwrite, create an account and create your first project.
{% only_dark %}
![Create project screen](/images/docs/quick-starts/dark/create-project.png)
{% /only_dark %}
{% only_light %}
![Create project screen](/images/docs/quick-starts/create-project.png)
{% /only_light %}
Then, under **Integrate with your server**, add an **API Key** with the following scopes.
{% only_dark %}
![Create project screen](/images/docs/quick-starts/dark/integrate-server.png)
{% /only_dark %}
{% only_light %}
![Create project screen](/images/docs/quick-starts/integrate-server.png)
{% /only_light %}
| Category {% width=120 %} | Required scopes | Purpose |
|-----------|-----------------------|---------|
| Database | `databases.write` | Allows API key to create, update, and delete [databases](/docs/products/databases/databases). |
| | `collections.write` | Allows API key to create, update, and delete [collections](/docs/products/databases/collections). |
| | `attributes.write` | Allows API key to create, update, and delete [attributes](/docs/products/databases/collections#attributes). |
| | `documents.read` | Allows API key to read [documents](/docs/products/databases/documents). |
| | `documents.write` | Allows API key to create, update, and delete [documents](/docs/products/databases/documents). |
Other scopes are optional.
{% /section %}
{% section #step-2 step=2 title="Create PHP project" %}
Create a PHP CLI application.
```sh
mkdir my-app
cd my-app
composer init
```
{% /section %}
{% section #step-3 step=3 title="Install Appwrite" %}
Install the PHP Appwrite SDK.
```sh
composer require appwrite/appwrite:11.0.1
```
{% /section %}
{% section #step-4 step=4 title="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.
{% only_dark %}
![Project settings screen](/images/docs/quick-starts/dark/project-id.png)
{% /only_dark %}
{% only_light %}
![Project settings screen](/images/docs/quick-starts/project-id.png)
{% /only_light %}
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>');
```
{% /section %}
{% section #step-5 step=5 title="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];
}
```
{% /section %}
{% section #step-6 step=6 title="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
);
}
```
{% /section %}
{% section #step-7 step=7 title="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);
```
{% /section %}
{% section #step-8 step=8 title="All set" %}
Run your project with `php src/index.php` and view the response in your console.
{% /section %}