PHP code example of innovation-studios / evolution-api-plugin

1. Go to this page and download the library: Download innovation-studios/evolution-api-plugin library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

innovation-studios / evolution-api-plugin example snippets


use EvolutionApiPlugin\EvolutionApi;

$apiKey = 'SUA_CHAVE_AQUI';
$apiUrl = 'SEU_URL_AQUI'; // Opcional, padrão: ''

$evolutionApi = new EvolutionApi($apiKey, $apiUrl);

$response = $evolutionApi->createInstance(
    'exampleInstance', // Nome da instância
    'SEU_TOKEN_AQUI', // Token
    true // Gerar QR Code
);

$response = $evolutionApi->sendTextMessage(
    '5511999999999', // Número
    'Olá, mundo!',   // Texto da mensagem
    [                // Opções (opcional)
        'delay' => 2,
        'presence' => 'composing',
    ],
    [                // Menções (opcional)
        'everyone' => false,
        'mentioned' => ['fulano', 'ciclano'],
    ]
);


$mediaMessage = [
    'mediatype' => 'image',
    'fileName' => 'foto.jpg',
    'caption' => 'Esta é uma imagem de exemplo',
    'media' => 'https://exemplo.com/foto.jpg', // URL ou base64 da mídia
];
$response = $evolutionApi->sendMediaMessage($instanceName, $number, $mediaMessage);


$listMessage = [
    'title' => 'Título da Lista',
    'description' => 'Descrição da Lista',
    'footerText' => 'Texto do Rodapé',
    'buttonText' => 'Texto do Botão',
    'sections' => [
        [
            'title' => 'Seção 1',
            'rows' => [
                [
                    'title' => 'Item 1',
                    'description' => 'Descrição do Item 1',
                    'rowId' => 'item1',
                ],
                [
                    'title' => 'Item 2',
                    'description' => 'Descrição do Item 2',
                    'rowId' => 'item2',
                ],
            ],
        ],
    ],
];

$response = $evolutionApi->sendList($instanceName, $number, $listMessage);

$response = $evolutionApi->fetchInstance('exampleInstance');

$response = $evolutionApi->deleteInstance('exampleInstance');

use EvolutionApiPlugin\EvolutionApi;

class ExampleController extends Controller
{
    protected $evolutionApi;

    public function __construct(EvolutionApi $evolutionApi)
    {
        $this->evolutionApi = $evolutionApi;
    }

    public function sendMessage()
    {
        $response = $this->evolutionApi->sendTextMessage(
            '5511999999999', // Número
            'Olá, mundo!'    // Texto da mensagem
        );

        return response()->json($response);
    }
}

use EvolutionApiPlugin\EvolutionApi;

$apiKey = 'SUA_CHAVE_AQUI';
## Evolution API Plugin

Biblioteca PHP para integrar aplicações (Laravel ou PHP puro) com a API Evolution.

Este README fornece:

- Instruções de instalação e configuração
- Exemplos completos de uso (Laravel e PHP puro)
- Referência detalhada da API pública da biblioteca (métodos, parâmetros, estruturas)
- Troubleshooting e FAQ

## Requisitos

- PHP >= 8.1
- Extensão cURL (usada pelo Guzzle)
- dependência: guzzlehttp/guzzle (já indicada no composer.json)

## Instalação

Instale via Composer:


use EvolutionApiPlugin\EvolutionApi;

$apiKey = 'SUA_CHAVE_AQUI';
$apiUrl = 'https://seu-evolution.example.com';

$evolution = new EvolutionApi($apiKey, $apiUrl);

$response = $evolution->sendTextMessage('exampleInstance', '5511999999999', 'Olá do Evolution!');
print_r($response);

$resp = $evolution->createInstance('meuInstance', 'token123', true);

$info = $evolution->fetchInstance('meuInstance');

$result = $evolution->deleteInstance('meuInstance');

$resp = $evolution->sendTextMessage('meuInstance', '5511999999999', 'Olá!', ['delay' => 2]);

$evolution->setApiVersion('v2');
$resp = $evolution->sendTextMessage('meuInstance', '5511999999999', 'Olá @fulano', ['linkPreview' => true], ['fulano'] );

$list = [
  'title' => 'Menu',
  'description' => 'Escolha uma opção',
  'buttonText' => 'Abrir',
  'footerText' => 'Footer',
  'sections' => [ [ 'title' => 'Seção', 'rows' => [ [ 'title' => 'Opção 1', 'rowId' => 'op1' ] ] ] ]
];

$resp = $evolution->sendList('meuInstance', '5511999999999', $list);

$media = [
  'mediatype' => 'image',
  'mimetype' => 'image/jpeg',
  'caption' => 'Legenda',
  'media' => 'https://exemplo.com/foto.jpg',
  'fileName' => 'foto.jpg'
];

$resp = $evolution->sendMediaMessage('meuInstance', '5511999999999', $media);



use EvolutionApiPlugin\EvolutionApi;

$apiKey = getenv('EVOLUTION_API_KEY') ?: 'SUA_CHAVE';
$apiUrl = getenv('EVOLUTION_API_URL') ?: 'https://seu-evolution.example.com';

$evolution = new EvolutionApi($apiKey, $apiUrl);

try {
    $instance = 'meuInstance';
    $resp = $evolution->sendTextMessage($instance, '5511999999999', 'Mensagem de teste');
    print_r($resp);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
    echo "Erro HTTP: " . $e->getMessage();
}

use EvolutionApiPlugin\EvolutionApi;

class EvolutionController extends Controller
{
    public function send(EvolutionApi $evolution)
    {
        $resp = $evolution->sendTextMessage('meuInstance', '5511999999999', 'Olá do Laravel');
        return response()->json($resp);
    }
}
bash
php artisan vendor:publish --tag=config