137 lines
4.9 KiB
PHP
Raw Permalink Normal View History

2022-09-10 16:01:51 +07:00
<?php
2024-06-26 17:39:05 +07:00
2023-10-12 15:55:42 +07:00
/**
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
* by https://t.me/ibnux
**/
2022-09-10 16:01:51 +07:00
/**
2023-10-12 15:55:42 +07:00
* using proxy, add this variable in config.php
* $http_proxy = '127.0.0.1:3128';
* if proxy using authentication, use this parameter
* $http_proxyauth = 'user:password';
2022-09-10 16:01:51 +07:00
**/
class Http
{
2025-02-11 13:07:38 +07:00
public static function getData($url, $headers = [], $connect_timeout = 3000, $wait_timeout = 3000)
2022-09-10 16:01:51 +07:00
{
2023-09-13 10:07:58 +07:00
global $http_proxy, $http_proxyauth, $admin;
2022-09-10 16:01:51 +07:00
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 0);
2025-02-11 13:07:38 +07:00
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connect_timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $wait_timeout);
2024-06-26 17:39:05 +07:00
if (is_array($headers) && count($headers) > 0) {
2023-09-27 15:01:48 +07:00
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
2022-09-10 16:01:51 +07:00
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
2023-09-13 10:07:58 +07:00
if (!empty($http_proxy)) {
2023-09-13 10:00:26 +07:00
curl_setopt($ch, CURLOPT_PROXY, $http_proxy);
2023-09-13 10:07:58 +07:00
if (!empty($http_proxyauth)) {
2023-09-13 10:00:26 +07:00
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $http_proxyauth);
}
}
2022-09-10 16:01:51 +07:00
$server_output = curl_exec($ch);
2023-09-13 10:00:26 +07:00
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
}
2022-09-10 16:01:51 +07:00
curl_close($ch);
2024-06-26 17:39:05 +07:00
if ($admin && $error_msg) {
Message::sendTelegram(
"Http::getData Error:\n" .
_get('_route') . "\n" .
"\n$url" .
"\n$error_msg"
);
return $error_msg;
2023-09-13 10:07:58 +07:00
}
2024-06-26 17:39:05 +07:00
return (!empty($server_output)) ? $server_output : $error_msg;
2022-09-10 16:01:51 +07:00
}
2025-02-11 13:07:38 +07:00
public static function postJsonData($url, $array_post, $headers = [], $basic = null, $connect_timeout = 3000, $wait_timeout = 3000)
2022-09-10 16:01:51 +07:00
{
2023-09-13 10:07:58 +07:00
global $http_proxy, $http_proxyauth, $admin;
2022-09-10 16:01:51 +07:00
$headers[] = 'Content-Type: application/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
2025-02-11 13:07:38 +07:00
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connect_timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $wait_timeout);
2022-09-10 16:01:51 +07:00
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLINFO_HEADER_OUT, false);
2023-09-13 10:07:58 +07:00
if (!empty($http_proxy)) {
2023-09-13 10:00:26 +07:00
curl_setopt($ch, CURLOPT_PROXY, $http_proxy);
2023-09-13 10:07:58 +07:00
if (!empty($http_proxyauth)) {
2023-09-13 10:00:26 +07:00
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $http_proxyauth);
}
}
2022-09-10 16:01:51 +07:00
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($array_post));
2024-06-26 17:39:05 +07:00
if (is_array($headers) && count($headers) > 0) {
2023-09-27 15:01:48 +07:00
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
2022-09-10 16:01:51 +07:00
if (!empty($basic)) {
curl_setopt($ch, CURLOPT_USERPWD, $basic);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
2023-09-13 10:00:26 +07:00
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
}
2022-09-10 16:01:51 +07:00
curl_close($ch);
2024-06-26 17:39:05 +07:00
if ($admin && $error_msg) {
Message::sendTelegram(
"Http::postJsonData:\n" .
_get('_route') . "\n" .
"\n$url" .
"\n$error_msg"
);
return $error_msg;
2023-09-13 10:07:58 +07:00
}
2024-06-26 17:39:05 +07:00
return (!empty($server_output)) ? $server_output : $error_msg;
2022-09-10 16:01:51 +07:00
}
2022-12-14 15:08:23 +07:00
2025-02-11 13:07:38 +07:00
public static function postData($url, $array_post, $headers = [], $basic = null, $connect_timeout = 3000, $wait_timeout = 3000)
2022-12-14 15:08:23 +07:00
{
2023-09-13 10:07:58 +07:00
global $http_proxy, $http_proxyauth, $admin;
2022-12-14 15:08:23 +07:00
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
2025-02-11 13:07:38 +07:00
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connect_timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $wait_timeout);
2022-12-14 15:08:23 +07:00
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLINFO_HEADER_OUT, false);
2023-09-13 10:07:58 +07:00
if (!empty($http_proxy)) {
2023-09-13 10:00:26 +07:00
curl_setopt($ch, CURLOPT_PROXY, $http_proxy);
2023-09-13 10:07:58 +07:00
if (!empty($http_proxyauth)) {
2023-09-13 10:00:26 +07:00
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $http_proxyauth);
}
}
2022-12-14 15:08:23 +07:00
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array_post));
2024-06-26 17:39:05 +07:00
if (is_array($headers) && count($headers) > 0) {
2023-09-27 15:01:48 +07:00
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
2022-12-14 15:08:23 +07:00
if (!empty($basic)) {
curl_setopt($ch, CURLOPT_USERPWD, $basic);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
2023-09-13 10:00:26 +07:00
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
}
2022-12-14 15:08:23 +07:00
curl_close($ch);
2024-06-26 17:39:05 +07:00
if ($admin && $error_msg) {
Message::sendTelegram(
"Http::postData Error:\n" .
_get('_route') . "\n" .
"\n$url" .
"\n$error_msg"
);
return $error_msg;
2023-09-13 10:07:58 +07:00
}
2024-06-26 17:39:05 +07:00
return (!empty($server_output)) ? $server_output : $error_msg;
2022-12-14 15:08:23 +07:00
}
2022-09-10 16:01:51 +07:00
}