Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 56 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Appwrite PHP SDK

![License](https://img.shields.io/github/license/appwrite/sdk-for-php.svg?style=flat-square&v=1)
![License](https://img.shields.io/badge/license-BSD--3--Clause-blue.svg?style=flat-square)
![Version](https://img.shields.io/badge/api%20version-2.5.0-blue.svg?style=flat-square&v=1)
[![Build Status](https://img.shields.io/travis/com/appwrite/sdk-generator?style=flat-square)](https://travis-ci.com/appwrite/sdk-generator)
[![Twitter Account](https://img.shields.io/twitter/follow/appwrite?color=00acee&label=twitter&style=flat-square)](https://twitter.com/appwrite)
[![X Account](https://img.shields.io/badge/follow-%40appwrite-000000?logo=x&style=flat-square)](https://x.com/appwrite)
[![Discord](https://img.shields.io/discord/564160730845151244?label=discord&style=flat-square)](https://appwrite.io/discord)

**This SDK targets Appwrite server version 2.5.x as shipped on Appwrite Cloud.** Self-hosted releases can lag behind Cloud — if you run an older self-hosted build, use a matching older SDK from [previous releases](https://github.com/appwrite/sdk-for-php/releases) when APIs differ.
Expand All @@ -14,7 +13,7 @@ Appwrite is an open-source backend as a service server that abstracts and simpli

## Installation

To install via [Composer](http://getcomposer.org/):
To install via [Composer](https://getcomposer.org/):

```bash
composer require appwrite/appwrite
Expand Down Expand Up @@ -49,76 +48,96 @@ $client->setEndpoint('https://cloud.appwrite.io/v1');

Configure timeouts, TLS, redirects, and connection reuse on the injected transport. The SDK's `setTimeout()`, `setConnectTimeout()`, and `setSelfSigned()` methods only configure the default cURL transport. Endpoint, authentication, headers, request encoding, and response handling remain the SDK's responsibility. A transport used with `responseType: 'location'` must have redirect following disabled.


## Getting Started

### Init your SDK
Initialize your SDK with your Appwrite server API endpoint and project ID which can be found in your project settings page and your new API secret Key from project's API keys section.
Initialize the SDK with your Appwrite endpoint, project ID, and an API key. Find the endpoint and project ID in your project's **Settings** page, and create an API key with the scopes your code needs under **Overview > Integrations > API keys**. API keys grant server-level access, so keep them out of client-side code.

```php
$client = new Client();

$client
->setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
->setProject('5df5acd0d48c2') // Your project ID
->setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
->setSelfSigned() // Use only on dev mode with a self-signed SSL cert
;
use Appwrite\Client;

$client = new Client()
->setEndpoint('https://<REGION>.cloud.appwrite.io/v1')
->setProject('<PROJECT_ID>')
->setKey('<API_KEY>');
```

### Make Your First Request
Once your SDK object is set, create any of the Appwrite service objects and choose any request to send. Full documentation for any service method you would like to use can be found in your SDK documentation or in the [API References](https://appwrite.io/docs) section.
If you self-host Appwrite, use your own endpoint, such as `https://appwrite.example.com/v1`. During local development with a self-signed certificate, call `->setSelfSigned()` to skip certificate verification. Never use it in production.

### Make your first request
Once the client is configured, create a service object and call any of its methods. Each method is documented in the [API references](https://appwrite.io/docs/references).

```php
use Appwrite\ID;
use Appwrite\Services\Users;

$users = new Users($client);

$user = $users->create(ID::unique(), "email@example.com", "+123456789", "password", "Walter O'Brien");
$user = $users->create(
userId: ID::unique(),
email: 'email@example.com',
password: 'password',
name: "Walter O'Brien",
);

echo $user->name;
```

### Full Example
### Full example
```php
<?php

require __DIR__ . '/vendor/autoload.php';

use Appwrite\Client;
use Appwrite\ID;
use Appwrite\Services\Users;

$client = new Client();

$client
->setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
->setProject('5df5acd0d48c2') // Your project ID
->setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
->setSelfSigned() // Use only on dev mode with a self-signed SSL cert
;
$client = new Client()
->setEndpoint('https://<REGION>.cloud.appwrite.io/v1')
->setProject('<PROJECT_ID>')
->setKey('<API_KEY>');

$users = new Users($client);

$user = $users->create(ID::unique(), "email@example.com", "+123456789", "password", "Walter O'Brien");
$user = $users->create(
userId: ID::unique(),
email: 'email@example.com',
password: 'password',
name: "Walter O'Brien",
);

echo $user->name;
```

### Error Handling
The Appwrite PHP SDK raises `AppwriteException` object with `message`, `code` and `response` properties. You can handle any errors by catching `AppwriteException` and present the `message` to the user or handle it yourself based on the provided error information. Below is an example.
### Error handling
The SDK throws `Appwrite\AppwriteException` when a request fails. Use `getMessage()`, `getCode()`, `getType()`, and `getResponse()` to inspect the error.

```php
$users = new Users($client);
use Appwrite\AppwriteException;

try {
$user = $users->create(ID::unique(), "email@example.com", "+123456789", "password", "Walter O'Brien");
} catch(AppwriteException $error) {
$user = $users->create(
userId: ID::unique(),
email: 'email@example.com',
password: 'password',
name: "Walter O'Brien",
);
} catch (AppwriteException $error) {
echo $error->getMessage();
}

```

### Learn more
You can use the following resources to learn more and get help
- 🚀 [Getting Started Tutorial](https://appwrite.io/docs/getting-started-for-server)
You can use the following resources to learn more and get help:
- 🚀 [PHP Quick Start](https://appwrite.io/docs/quick-starts/php)
- 📜 [Appwrite Docs](https://appwrite.io/docs)
- 💬 [Discord Community](https://appwrite.io/discord)
- 🚂 [Appwrite PHP Playground](https://github.com/appwrite/playground-for-php)


## Contribution

This library is auto-generated by Appwrite custom [SDK Generator](https://github.com/appwrite/sdk-generator). To learn more about how you can help us improve this SDK, please check the [contribution guide](https://github.com/appwrite/sdk-generator/blob/master/CONTRIBUTING.md) before sending a pull-request.
This library is auto-generated by Appwrite custom [SDK Generator](https://github.com/appwrite/sdk-generator). To learn more about how you can help us improve this SDK, please check the [contribution guide](https://github.com/appwrite/sdk-generator/blob/main/CONTRIBUTING.md) before sending a pull-request.

## License

Expand Down
Loading