-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
634 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"symbol-whitelist" : [], | ||
"php-core-extensions" : [ | ||
"Core", | ||
"date", | ||
"json", | ||
"hash", | ||
"pcre", | ||
"Phar", | ||
"Reflection", | ||
"SPL", | ||
"random", | ||
"standard", | ||
"curl" | ||
], | ||
"scan-files" : [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Transport | ||
|
||
`TelegramBotApi` requires a transport implementation to make requests to the Telegram Bot API. Out of the box, available two transport implementations: cURL and PSR. | ||
|
||
## cURL | ||
|
||
The `CurlTransport` class is a transport implementation for making requests to the Telegram Bot API using the [cURL](https://www.php.net/manual/book.curl.php) extension in PHP. This transport is often the easiest choice since the cURL extension is included in most PHP installations. | ||
|
||
General usage: | ||
|
||
```php | ||
use Vjik\TelegramBot\Api\TelegramBotApi; | ||
use Vjik\TelegramBot\Api\Transport\Curl\CurlTransport; | ||
|
||
$transport = new CurlTransport( | ||
// Telegram bot authentication token | ||
'110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw' | ||
); | ||
|
||
$api = new TelegramBotApi($transport); | ||
``` | ||
|
||
Constructor parameters: | ||
|
||
- `$token` — Telegram bot authentication token; | ||
- `$baseUrl` — the base URL for Telegram Bot API requests (default `https://api.telegram.org`). | ||
|
||
## PSR | ||
|
||
PSR transport requires the [PSR-18](https://www.php-fig.org/psr/psr-18/) HTTP client and [PSR-17](https://www.php-fig.org/psr/psr-17/) HTTP factories. | ||
|
||
For example, you can use the [php-http/curl-client](https://github.com/php-http/curl-client) and [httpsoft/http-message](https://github.com/httpsoft/http-message): | ||
|
||
```shell | ||
composer require php-http/curl-client httpsoft/http-message | ||
``` | ||
|
||
General usage: | ||
|
||
```php | ||
use Http\Client\Curl\Client; | ||
use HttpSoft\Message\RequestFactory; | ||
use HttpSoft\Message\ResponseFactory; | ||
use HttpSoft\Message\StreamFactory; | ||
use Vjik\TelegramBot\Api\TelegramBotApi; | ||
use Vjik\TelegramBot\Api\Transport\PsrTransport; | ||
|
||
$streamFactory = new StreamFactory(); | ||
$responseFactory = new ResponseFactory(); | ||
$requestFactory = new RequestFactory(); | ||
$client = new Client($responseFactory, $streamFactory); | ||
|
||
$transport = new PsrTransport( | ||
$token, | ||
$client, | ||
$requestFactory, | ||
$streamFactory, | ||
); | ||
|
||
$api = new TelegramBotApi($transport); | ||
``` | ||
|
||
Constructor parameters: | ||
|
||
- `$token` — Telegram bot authentication token; | ||
- `$client` — PSR-18 HTTP client; | ||
- `$requestFactory` — PSR-17 HTTP request factory; | ||
- `$streamFactory` — PSR-17 HTTP stream factory; | ||
- `$baseUrl` — the base URL for Telegram Bot API requests (default `https://api.telegram.org`). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Vjik\TelegramBot\Api\Transport\Curl; | ||
|
||
use CurlHandle; | ||
use RuntimeException; | ||
|
||
use function curl_close; | ||
use function curl_errno; | ||
use function curl_error; | ||
use function curl_exec; | ||
use function curl_getinfo; | ||
use function curl_init; | ||
use function curl_setopt_array; | ||
|
||
/** | ||
* @internal | ||
* @codeCoverageIgnore | ||
*/ | ||
final class Curl implements CurlInterface | ||
{ | ||
public function close(CurlHandle $handle): void | ||
{ | ||
curl_close($handle); | ||
} | ||
|
||
public function exec(CurlHandle $handle): ?string | ||
{ | ||
$result = curl_exec($handle); | ||
if ($result === false) { | ||
throw new RuntimeException( | ||
'CURL error: ' . curl_error($handle), | ||
curl_errno($handle), | ||
); | ||
} | ||
|
||
return $result === true ? null : $result; | ||
} | ||
|
||
public function getinfo(CurlHandle $handle, ?int $option = null): mixed | ||
{ | ||
return curl_getinfo($handle, $option); | ||
} | ||
|
||
public function init(): CurlHandle | ||
{ | ||
$result = curl_init(); | ||
if ($result === false) { | ||
throw new RuntimeException('Failed to initialize CURL.'); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
public function setopt_array(CurlHandle $handle, array $options): void | ||
{ | ||
$result = curl_setopt_array($handle, $options); | ||
if ($result === false) { | ||
throw new RuntimeException('Failed to set CURL options.'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Vjik\TelegramBot\Api\Transport\Curl; | ||
|
||
use CurlHandle; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
interface CurlInterface | ||
{ | ||
public function close(CurlHandle $handle): void; | ||
|
||
public function exec(CurlHandle $handle): ?string; | ||
|
||
public function getinfo(CurlHandle $handle, ?int $option = null): mixed; | ||
|
||
public function init(): CurlHandle; | ||
|
||
public function setopt_array(CurlHandle $handle, array $options): void; | ||
} |
Oops, something went wrong.