Skip to content

Latest commit

 

History

History
69 lines (45 loc) · 2.39 KB

File metadata and controls

69 lines (45 loc) · 2.39 KB

Transport

By default TelegramBotApi uses cURL to make requests to the Telegram Bot API and download files from Telegram servers. But you can use any other transport implementation that implements the Phptg\BotApi\Transport\TransportInterface interface.

Out of the box, available two transport implementations: cURL and native.

Additionally, the phptg/transport-psr package provides PsrTransport based on PSR-18 HTTP client and PSR-17 HTTP factories.

cURL

The CurlTransport class is a transport implementation for making requests to the Telegram Bot API using the cURL extension in PHP. This transport is often the easiest choice since the cURL extension is included in most PHP installations.

General usage:

use Phptg\BotApi\TelegramBotApi;
use Phptg\BotApi\Transport\CurlTransport;

// Telegram bot authentication token
$token = '110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw';

$transport = new CurlTransport();

$api = new TelegramBotApi($token, transport: $transport);

Constructor parameters:

  • $mimeTypeResolverMIME type resolver for determining file types. Defaults to ApacheMimeTypeResolver.

Native

The NativeTransport uses native PHP functions file_get_contents() and file_put_contents() to make requests to the Telegram Bot API and not require any additional extensions.

Note: NativeTransport requires that allow_url_fopen option be enabled.

General usage:

use Phptg\BotApi\TelegramBotApi;
use Phptg\BotApi\Transport\NativeTransport;

// Telegram bot authentication token
$token = '110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw';

$transport = new NativeTransport();

$api = new TelegramBotApi($token, transport: $transport);

Constructor parameters:

  • $mimeTypeResolverMIME type resolver for determining file types. Defaults to ApacheMimeTypeResolver.

Available MIME type resolvers:

  • ApacheMimeTypeResolver - based on file extension and Apache's mime.types file (uses by default);
  • CustomMimeTypeResolver - based on file extension and custom MIME types map;
  • CompositeMimeTypeResolver - allows to combine multiple resolvers into one.