add proxy to connection

This commit is contained in:
Ibnu Maksum
2023-09-13 10:00:26 +07:00
parent e3ec8b18fa
commit ee2e67f490
5 changed files with 99 additions and 6 deletions

View File

@ -2,12 +2,17 @@
/**
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
* 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';
**/
class Http
{
public static function getData($url, $headers = [])
{
global $http_proxy,$http_proxyauth;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 0);
@ -15,13 +20,23 @@ class Http
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if(!empty($http_proxy)){
curl_setopt($ch, CURLOPT_PROXY, $http_proxy);
if(!empty($http_proxyauth)){
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $http_proxyauth);
}
}
$server_output = curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
}
curl_close($ch);
return $server_output;
return ($server_output) ? $server_output : $error_msg;
}
public static function postJsonData($url, $array_post, $headers = [], $basic = null)
{
global $http_proxy,$http_proxyauth;
$headers[] = 'Content-Type: application/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
@ -30,6 +45,12 @@ class Http
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLINFO_HEADER_OUT, false);
if(!empty($http_proxy)){
curl_setopt($ch, CURLOPT_PROXY, $http_proxy);
if(!empty($http_proxyauth)){
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $http_proxyauth);
}
}
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($array_post));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
if (!empty($basic)) {
@ -37,13 +58,17 @@ class Http
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
}
curl_close($ch);
return $server_output;
return ($server_output) ? $server_output : $error_msg;
}
public static function postData($url, $array_post, $headers = [], $basic = null)
{
global $http_proxy,$http_proxyauth;
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
@ -52,6 +77,12 @@ class Http
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLINFO_HEADER_OUT, false);
if(!empty($http_proxy)){
curl_setopt($ch, CURLOPT_PROXY, $http_proxy);
if(!empty($http_proxyauth)){
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $http_proxyauth);
}
}
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array_post));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
if (!empty($basic)) {
@ -59,7 +90,10 @@ class Http
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
}
curl_close($ch);
return $server_output;
return ($server_output) ? $server_output : $error_msg;
}
}