Initial commit

This commit is contained in:
Fill committed 2023-04-10 13:51:00 +03:00
commit c8acf0604b
7 files changed
+1179

No files matched your search

+168
View File
@@ -0,0 +1,168 @@
<?php
/**
* Curl Helper.
*
* @author Ivan Filipov <ivan@web-lab.solutions>
*
* @version 1.0
* @date Feb 02, 2021
* @date updated Feb 02, 2021
*/
namespace wlurep;
/**
* Sets some default functions and settings.
*/
class SimpleCurl {
const defaultTimeout = 5;
/**
* Timeout.
*
* timeout in seconds. Can be overriden
*
* @since 1.0.0
* @access public
* @static
*
* @var timeout
*/
public static $timeout = self::defaultTimeout;
/**
* Performs a get request on the chosen link and the chosen parameters
* in the array.
*
* @param string $url
* @param array $params
*
* @return string returns the content of the given url
*/
public static function get($url, $params = []) {
if (is_array($params) && count($params) > 0) {
$url = $url . '?' . http_build_query($params, '', '&');
}
$ch = curl_init();
$options = [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => self::$timeout,
CURLOPT_SSL_VERIFYPEER => false,
];
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
/**
* Performs a post request on the chosen link and the chosen parameters
* in the array.
*
* @param string $url
* @param array $fields
*
* @return string returns the content of the given url after post
*/
public static function post($url, $fields = []) {
if (is_array($fields) && count($fields) > 0) {
$postFieldsString = json_encode($fields);
} else {
$postFieldsString = '';
}
# The maximum execution time, in seconds. If set to zero, no time limit is imposed.
set_time_limit(0);
# Make sure to keep alive the script when a client disconnect.
ignore_user_abort(true);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Connection: Keep-Alive',
'Accept-Encoding: gzip, deflate',
'Accept-Language: en-US,en;q=0.5',
'Cache-Control: no-cache',
'Referer: http://erp.my', //Your referrer address
'Keep-Alive: timeout=100, max=1000'
));
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_KEEP_SENDING_ON_ERROR, true);
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, self::$timeout);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFieldsString);
curl_setopt($ch, CURLOPT_POST, 1); // This is a POST request
$response = curl_exec($ch);
$curlinfo = curl_getinfo($ch);
curl_close($ch);
return $response;
}
/**
* Performs a put request on the chosen link and the chosen parameters
* in the array.
*
* @param string $url
* @param array $fields
*
* @return string with the contents of the site
*/
public static function put($url, $fields = []) {
if (is_array($fields) && count($fields) > 0) {
$postFieldsString = http_build_query($fields, '', '&');
} else {
$postFieldsString = '';
}
$ch = curl_init($url);
$options = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => self::$timeout,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS => $postFieldsString,
];
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
/**
* Performs a ping request on the chosen link
* in the array.
*
* @param string $url
*
* @return bool
*/
public static function ping($url = '') {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, self::$timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpcode >= 200 && $httpcode < 300) {
return true;
}
return false;
}
}