phpnuxbill/system/autoload/Http.php

100 lines
3.6 KiB
PHP
Raw Normal View History

2022-09-10 17:01:51 +08:00
<?php
/**
2022-10-16 15:50:24 +08:00
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
2023-09-13 11:00:26 +08: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 17:01:51 +08:00
**/
class Http
{
public static function getData($url, $headers = [])
2022-09-10 17:01:51 +08:00
{
2023-09-13 11:00:26 +08:00
global $http_proxy,$http_proxyauth;
2022-09-10 17:01:51 +08:00
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
2023-09-13 11:00:26 +08:00
if(!empty($http_proxy)){
curl_setopt($ch, CURLOPT_PROXY, $http_proxy);
if(!empty($http_proxyauth)){
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $http_proxyauth);
}
}
2022-09-10 17:01:51 +08:00
$server_output = curl_exec($ch);
2023-09-13 11:00:26 +08:00
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
}
2022-09-10 17:01:51 +08:00
curl_close($ch);
2023-09-13 11:00:26 +08:00
return ($server_output) ? $server_output : $error_msg;
2022-09-10 17:01:51 +08:00
}
public static function postJsonData($url, $array_post, $headers = [], $basic = null)
{
2023-09-13 11:00:26 +08:00
global $http_proxy,$http_proxyauth;
2022-09-10 17:01:51 +08:00
$headers[] = 'Content-Type: application/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLINFO_HEADER_OUT, false);
2023-09-13 11:00:26 +08:00
if(!empty($http_proxy)){
curl_setopt($ch, CURLOPT_PROXY, $http_proxy);
if(!empty($http_proxyauth)){
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $http_proxyauth);
}
}
2022-09-10 17:01:51 +08:00
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($array_post));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
if (!empty($basic)) {
curl_setopt($ch, CURLOPT_USERPWD, $basic);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
2023-09-13 11:00:26 +08:00
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
}
2022-09-10 17:01:51 +08:00
curl_close($ch);
2023-09-13 11:00:26 +08:00
return ($server_output) ? $server_output : $error_msg;
2022-09-10 17:01:51 +08:00
}
2022-12-14 16:08:23 +08:00
public static function postData($url, $array_post, $headers = [], $basic = null)
{
2023-09-13 11:00:26 +08:00
global $http_proxy,$http_proxyauth;
2022-12-14 16:08:23 +08:00
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLINFO_HEADER_OUT, false);
2023-09-13 11:00:26 +08:00
if(!empty($http_proxy)){
curl_setopt($ch, CURLOPT_PROXY, $http_proxy);
if(!empty($http_proxyauth)){
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $http_proxyauth);
}
}
2022-12-14 16:08:23 +08:00
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array_post));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
if (!empty($basic)) {
curl_setopt($ch, CURLOPT_USERPWD, $basic);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
2023-09-13 11:00:26 +08:00
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
}
2022-12-14 16:08:23 +08:00
curl_close($ch);
2023-09-13 11:00:26 +08:00
return ($server_output) ? $server_output : $error_msg;
2022-12-14 16:08:23 +08:00
}
2022-09-10 17:01:51 +08:00
}