Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ php -S localhost:8080
### How to initialize an object

```
$greenApi = new GreenApiClient( ID_INSTANCE, API_TOKEN_INSTANCE );
$greenApi = new GreenApiClient( idInstance: ID_INSTANCE, apiTokenInstance: API_TOKEN_INSTANCE );
```

### Sending a text message to a WhatsApp number
Expand Down Expand Up @@ -183,6 +183,9 @@ This method will be called when an incoming message is received. Next, process m
| `statuses.getIncomingStatuses` | The method returns the incoming status messages of the instance | [GetIncomingStatuses](https://green-api.com/en/docs/api/statuses/GetIncomingStatuses/) |
| `statuses.getOutgoingStatuses` | The method returns the outgoing statuses of the account | [GetOutgoingStatuses](https://green-api.com/en/docs/api/statuses/GetOutgoingStatuses/) |
| `statuses.getStatusStatistic` | The method returns an array of recipients marked sent/delivered/read for a given status | [GetStatusStatistic](https://green-api.com/en/docs/api/statuses/GetStatusStatistic/) |
| `partner.getInstances` | The method is designed to get the instances of the account | [GetInstances](https://green-api.com/en/docs/partners/getInstances/) |
| `partner.createInstance` | The method is designed to create the instance in the account | [CreateInstance](https://green-api.com/en/docs/partners/createInstance/) |
| `partner.deleteInstanceAccount` | The method is designed to remove the instance in the account | [GetStatusStatistic](https://green-api.com/en/docs/partners/deleteInstanceAccount/) |

## Service methods documentation

Expand Down
5 changes: 4 additions & 1 deletion README_RUS.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ php -S localhost:8080
### Как инициализировать объект

```
$greenApi = new GreenApiClient( ID_INSTANCE, API_TOKEN_INSTANCE );
$greenApi = new GreenApiClient( idInstance: ID_INSTANCE, apiTokenInstance: API_TOKEN_INSTANCE );
```

### Отправка текстового сообщения на номер WhatsApp
Expand Down Expand Up @@ -181,6 +181,9 @@ body | тело сообщения (json)
| `statuses.getIncomingStatuses` | Метод возвращает крайние входящие статусы аккаунта | [GetIncomingStatuses](https://green-api.com/docs/api/statuses/GetIncomingStatuses/) |
| `statuses.getOutgoingStatuses` | Метод возвращает крайние отправленные статусы аккаунта | [GetOutgoingStatuses](https://green-api.com/docs/api/statuses/GetOutgoingStatuses/) |
| `statuses.getStatusStatistic` | Метод возвращает массив получателей со статусами, отмеченных как отправлено/доставлено/прочитано, для данного статуса | [GetStatusStatistic](https://green-api.com/docs/api/statuses/GetStatusStatistic/) |
| `partner.getInstances` | Метод предназначен для получения инстансов аккаунта | [GetInstances](https://green-api.com/en/docs/partners/getInstances/) |
| `partner.createInstance` | Метод предназначен для создания инстанса в аккаунте | [CreateInstance](https://green-api.com/en/docs/partners/createInstance/) |
| `partner.deleteInstanceAccount` | Метод предназначен для удаления инстанса в аккаунте | [GetStatusStatistic](https://green-api.com/en/docs/partners/deleteInstanceAccount/) |

## Документация по методам сервиса

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "green-api/whatsapp-api-client-php",
"name": "yuri-kovalev/whatsapp-api-client-php",
"description": "Green-api.com REST API Client",
"keywords": [
"green-api.com",
Expand Down
69 changes: 41 additions & 28 deletions src/GreenApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,72 +19,85 @@
use GreenApi\RestApi\tools\ServiceMethods;
use GreenApi\RestApi\tools\Webhooks;
use GreenApi\RestApi\tools\Statuses;
use GreenApi\RestApi\tools\Partner;
use stdClass;

class GreenApiClient {
private $host;
private $media;
private $idInstance;
private $apiTokenInstance;
private $idInstance = null;
private $apiTokenInstance = null;
private $partnerToken = null;

/**
* @var Account
* @var Account|null
*/
public $account;
/**
* @var Sending
* @var Sending|null
*/
public $sending;
/**
* @var Groups
* @var Groups|null
*/
public $groups;
/**
* @var Journals
* @var Journals|null
*/
public $journals;
/**
* @var Marking
* @var Marking|null
*/
public $marking;
/**
* @var Queues
* @var Queues|null
*/
public $queues;
/**
* @var Receiving
* @var Receiving|null
*/
public $receiving;
/**
* @var ServiceMethods
* @var ServiceMethods|null
*/
public $serviceMethods;
/**
* @var Webhooks
* @var Webhooks|null
*/
public $webhooks;
/**
* @var Statuses
* @var Statuses|null
*/
public $statuses;
/**
* @var Partner|null
*/
public $partner;

public function __construct( $idInstance, $apiTokenInstance, $host = "https://api.green-api.com", $media = "https://media.green-api.com" ) {
public function __construct( $idInstance = null, $apiTokenInstance = null, $partnerToken = null, $host = "https://api.green-api.com", $media = "https://media.green-api.com" ) {
Copy link
Author

@yuri-kovalev yuri-kovalev Jan 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any way php support named arguments from 8 version
new GreenApiClient(idInstance: 'idInstance', apiTokenInstance: 'apiTokenInstance', partnerToken: 'partnerToken')


$this->idInstance = $idInstance;
$this->apiTokenInstance = $apiTokenInstance;
$this->host = $host;
$this->idInstance = $idInstance;
$this->apiTokenInstance = $apiTokenInstance;
$this->host = $host;
$this->media = $media;

$this->account = new Account( $this );
$this->groups = new Groups( $this );
$this->journals = new Journals( $this );
$this->marking = new Marking( $this );
$this->queues = new Queues( $this );
$this->receiving = new Receiving( $this );
$this->sending = new Sending( $this );
$this->serviceMethods = new ServiceMethods( $this );
$this->webhooks = new Webhooks( $this );
$this->statuses = new Statuses( $this );
$this->partnerToken = $partnerToken;

if ($this->idInstance && $this->apiTokenInstance) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this, but for partner API there is no need to use idInstance and apiTokenInstance because we may not have them.

$this->account = new Account( $this );
$this->groups = new Groups( $this );
$this->journals = new Journals( $this );
$this->marking = new Marking( $this );
$this->queues = new Queues( $this );
$this->receiving = new Receiving( $this );
$this->sending = new Sending( $this );
$this->serviceMethods = new ServiceMethods( $this );
$this->webhooks = new Webhooks( $this );
$this->statuses = new Statuses( $this );
}

if ($this->partnerToken) {
$this->partner = new Partner( $this );
}
}

/**
Expand Down Expand Up @@ -144,7 +157,7 @@ public function request( string $method, string $url, array $payload = null, boo
break;
case 'POST_BINARY':
$mime_type = mime_content_type( $path );
$headers = array(
$headers = array(
'Content-Type: ' . $mime_type ,
'GA-Filename: ' . basename($path)
);
Expand Down
79 changes: 79 additions & 0 deletions src/tools/Partner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace GreenApi\RestApi\tools;

use GreenApi\RestApi\GreenApiClient;
use stdClass;

class Partner {

/**
* @param GreenApiClient $greenApi
*/
private $greenApi;

/**
* @param GreenApiClient $greenApi
*/
public function __construct(GreenApiClient $greenApi) {
$this->greenApi = $greenApi;
}

/**
* The method is used to getting all the account instances created by the partner.
*
* @return stdClass
* @link https://green-api.com/en/docs/partners/getInstances/
*/
public function getInstances(): stdClass {
return $this->greenApi->request('GET',
'{{host}}/partner/getInstances/{{partnerToken}}');
}

/**
* The method is used to creating a messenger account instance on the partner's part
*
* @param $payload
* @return stdClass
* @link https://green-api.com/en/docs/partners/createInstance/
*/
public function createInstance($payload): stdClass {
$defaultParameters = [
'name' => null,
'webhookUrl' => null,
'webhookUrlToken' => null,
'delaySendMessagesMilliseconds' => null,
'markIncomingMessagesReaded' => null,
'markIncomingMessagesReadedOnReply' => null,
'outgoingWebhook' => null,
'outgoingMessageWebhook' => null,
'outgoingAPIMessageWebhook' => null,
'stateWebhook' => null,
'incomingWebhook' => 'yes',
'deviceWebhook' => null,
'keepOnlineStatus' => null,
'pollMessageWebhook' => null,
'incomingBlockWebhook' => null,
'incomingCallWebhook' => null,
'editedMessageWebhook' => null,
'deletedMessageWebhook' => null,
];

$requestParameters = array_filter(array_merge($defaultParameters, $payload));

return $this->greenApi->request('GET',
'{{host}}/partner/createInstance/{{partnerToken}}', $requestParameters);
}

/**
*
* The method is used to deleting an instance of the partner's account.
*
* @return stdClass
* @link https://green-api.com/en/docs/partners/deleteInstanceAccount/
*/
public function deleteInstanceAccount(): stdClass {
return $this->greenApi->request('GET',
'{{host}}/partner/deleteInstanceAccount/{{partnerToken}}');
}
}