PHP code example of phptg / transport-psr

1. Go to this page and download the library: Download phptg/transport-psr 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/ */

    

phptg / transport-psr example snippets


use Http\Client\Curl\Client;
use HttpSoft\Message\RequestFactory;
use HttpSoft\Message\ResponseFactory;
use HttpSoft\Message\StreamFactory;
use Phptg\BotApi\TelegramBotApi;
use Phptg\TransportPsr\PsrTransport;

$streamFactory = new StreamFactory();
$responseFactory = new ResponseFactory();
$requestFactory = new RequestFactory();
$client = new Client($responseFactory, $streamFactory);

$transport = new PsrTransport(
    $client,
    $requestFactory,
    $streamFactory,
);

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

// Now you can use the API as usual
$api->sendMessage(
    chatId: 123456789,
    text: 'Hello from PSR transport!',
);

use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Phptg\BotApi\Method\SendMessage;
use Phptg\BotApi\WebhookResponse\PsrWebhookResponseFactory;
use Phptg\BotApi\WebhookResponse\WebhookResponse;

/**
 * @var ResponseFactoryInterface $responseFactory
 * @var StreamFactoryInterface $streamFactory
 */

$factory = new PsrWebhookResponseFactory($responseFactory, $streamFactory);

// Create response from WebhookResponse object
$webhookResponse = new WebhookResponse(new SendMessage(chatId: 12345, text: 'Hello!'));
$response = $factory->create($webhookResponse);

// Or create response directly from method, if you are sure that InputFile is not used
$method = new SendMessage(chatId: 12345, text: 'Hello!');
$response = $factory->byMethod($method);

use Phptg\TransportPsr\PsrUpdateFactory;
use Psr\Http\Message\ServerRequestInterface;

/**
 * @var ServerRequestInterface $request
 */

$update = PsrUpdateFactory::create($request);

// Now you can use the Update object
$message = $update->message;