An open API service indexing awesome lists of open source software.

https://github.com/phpjuice/wati-http-client

PHP Http Client for Wati.io Rest API
https://github.com/phpjuice/wati-http-client

wati whatsapp whatsapp-api

Last synced: 6 days ago
JSON representation

PHP Http Client for Wati.io Rest API

Awesome Lists containing this project

README

          

# Wati HTTP Client

[![CI](https://github.com/phpjuice/wati-http-client/actions/workflows/php.yml/badge.svg)](https://github.com/phpjuice/wati-http-client/actions/workflows/php.yml)
[![PHP Version](https://img.shields.io/badge/PHP-8.3%20%7C%208.4-777BB4?logo=php&logoColor=white)](https://php.net)
[![Latest Stable Version](http://poser.pugx.org/phpjuice/wati-http-client/v)](https://packagist.org/packages/phpjuice/wati-http-client)
[![Total Downloads](http://poser.pugx.org/phpjuice/wati-http-client/downloads)](https://packagist.org/packages/phpjuice/wati-http-client)
[![License](https://img.shields.io/badge/License-MIT-brightgreen.svg)](https://opensource.org/licenses/MIT)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](./.github/CONTRIBUTING.md)

A PHP HTTP Client for the [Wati.io](https://wati.io) WhatsApp API. Provides a simple, fluent API to interact with Wati's
REST API.

## Related Packages

For a higher-level, feature-rich integration, consider using [wati-sdk](https://github.com/phpjuice/wati-sdk). It provides ready-to-use request classes, response DTOs, and additional convenience features built on top of this HTTP client.

## Installation

This package requires PHP 8.3 or higher.

```bash
composer require "phpjuice/wati-http-client"
```

## Setup

### Get Your Credentials

1. Log in to your [Wati Account](https://app.wati.io)
2. Navigate to **API Docs** in the top menu
3. Copy your **API Endpoint URL** and **Bearer Token**

### Create a Client

```php
60, // Request timeout in seconds (default: 30)
'connect_timeout' => 15, // Connection timeout in seconds (default: 10)
'verify' => true, // Verify SSL certificate (default: true)
'proxy' => 'tcp://localhost:8080', // Proxy URL (default: null)
'debug' => false, // Enable debug mode (default: false)
]);
```

## Usage

### Making Requests

Extend `WatiRequest` to create your API requests:

```php
'application/json']
);
}
}

class SendTemplateMessageRequest extends WatiRequest
{
public function __construct(string $phoneNumber, string $templateName, array $parameters = [])
{
$body = json_encode([
'template_name' => $templateName,
'broadcast_name' => $templateName,
'parameters' => $parameters,
]);

parent::__construct(
'POST',
"/api/v1/sendTemplateMessage?whatsappNumber={$phoneNumber}",
[
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
Utils::streamFor($body)
);
}
}
```

### Execute Requests

```php
send(new GetContactsRequest());
$data = Utils::jsonDecode($response->getBody()->getContents(), true);

// Send a template message
$response = $client->send(new SendTemplateMessageRequest(
'1234567890',
'hello_world',
['name' => 'John']
));
```

## API Reference

For full API documentation, visit [Wati API Docs](https://docs.wati.io/reference/introduction).

### Available Endpoints

- **Messaging**: Send templates, session messages, interactive messages
- **Contacts**: Get, add, update contacts
- **Conversations**: Messages, status updates
- **Templates**: Get and send message templates
- **Campaigns**: Manage broadcasts

## Error Handling

The client throws specific exceptions for different error scenarios:

```php
send(new GetContactsRequest());
} catch (AuthenticationException $e) {
// Invalid bearer token - check credentials
echo "Auth failed: " . $e->getMessage();
} catch (RateLimitException $e) {
// Rate limited - wait and retry
$retryAfter = $e->getRetryAfter(); // seconds to wait
} catch (ValidationException $e) {
// Invalid request parameters
$errors = $e->getResponseData();
} catch (WatiApiException $e) {
// Other API errors (4xx, 5xx)
$statusCode = $e->getStatusCode();
$data = $e->getResponseData();
} catch (WatiException $e) {
// Connection or other HTTP errors
echo "Request failed: " . $e->getMessage();
}
```

## Changelog

Please see the [CHANGELOG](changelog.md) for more information on what has changed recently.

## Contributing

Please see [CONTRIBUTING](./.github/CONTRIBUTING.md) for details.

## Security

If you discover any security-related issues, please email the author instead of using the issue tracker.

## License

Please see the [License](./LICENSE) file.