Skip to content

Commit d9f896e

Browse files
author
ggrillo
committed
Proxy support, but needs more testing
1 parent 9dc81b6 commit d9f896e

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

Telegram.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,23 @@ class Telegram
6868
private $data = [];
6969
private $updates = [];
7070
private $log_errors;
71+
private $proxy;
7172

7273
/// Class constructor
7374

7475
/**
7576
* Create a Telegram instance from the bot token
7677
* \param $bot_token the bot token
78+
* \param $log_errors enable or disable the logging
79+
* \param $proxy array with the proxy configuration (url, port, type, auth)
7780
* \return an instance of the class.
7881
*/
79-
public function __construct($bot_token, $log_errors = true)
82+
public function __construct($bot_token, $log_errors = true, array $proxy=array())
8083
{
8184
$this->bot_token = $bot_token;
8285
$this->data = $this->getData();
8386
$this->log_errors = $log_errors;
87+
$this->proxy = $proxy;
8488
}
8589

8690
/// Do requests to Telegram Bot API
@@ -3058,11 +3062,34 @@ private function sendAPIRequest($url, array $content, $post = true)
30583062
curl_setopt($ch, CURLOPT_POST, 1);
30593063
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
30603064
}
3065+
echo "inside curl if";
3066+
if (!empty($this->proxy)) {
3067+
echo "inside proxy if";
3068+
if (array_key_exists("type", $this->proxy)) {
3069+
curl_setopt($ch, CURLOPT_PROXYTYPE, $this->proxy["type"]);
3070+
}
3071+
3072+
if (array_key_exists("auth", $this->proxy)) {
3073+
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->proxy["auth"]);
3074+
}
3075+
3076+
if (array_key_exists("url", $this->proxy)) {
3077+
echo "Proxy Url";
3078+
curl_setopt($ch, CURLOPT_PROXY, $this->proxy["url"]);
3079+
}
3080+
3081+
if (array_key_exists("port", $this->proxy)) {
3082+
echo "Proxy port";
3083+
curl_setopt($ch, CURLOPT_PROXYPORT, $this->proxy["port"]);
3084+
}
3085+
3086+
}
30613087
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
30623088
$result = curl_exec($ch);
30633089
if ($result === false) {
30643090
$result = json_encode(['ok'=>false, 'curl_error_code' => curl_errno($ch), 'curl_error' => curl_error($ch)]);
30653091
}
3092+
echo $result;
30663093
curl_close($ch);
30673094
if ($this->log_errors) {
30683095
if (class_exists('TelegramErrorLogger')) {

0 commit comments

Comments
 (0)