YGGo/library/curl.php

94 lines
2.6 KiB
PHP
Raw Normal View History

2023-04-01 16:29:39 +00:00
<?php
class Curl {
private $_connection;
2023-04-03 01:47:31 +00:00
private $_response;
2023-04-01 16:29:39 +00:00
public function __construct(string $url,
mixed $userAgent = false,
int $connectTimeout = 10,
bool $header = false,
bool $followLocation = false,
int $maxRedirects = 10,
bool $sslVerifyHost = false,
bool $sslVerifyPeer = false) {
2023-04-01 16:29:39 +00:00
$this->_connection = curl_init($url);
if ($header) {
curl_setopt($this->_connection, CURLOPT_HEADER, true);
}
if ($followLocation) {
curl_setopt($this->_connection, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->_connection, CURLOPT_MAXREDIRS, $maxRedirects);
}
2023-04-01 16:29:39 +00:00
curl_setopt($this->_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->_connection, CURLOPT_CONNECTTIMEOUT, $connectTimeout); // skip resources with long time response
curl_setopt($this->_connection, CURLOPT_TIMEOUT, $connectTimeout); // prevent infinitive connection on streaming resources detected @TODO
curl_setopt($this->_connection, CURLOPT_SSL_VERIFYHOST, $sslVerifyHost);
curl_setopt($this->_connection, CURLOPT_SSL_VERIFYPEER, $sslVerifyPeer);
curl_setopt($this->_connection, CURLOPT_NOPROGRESS, false);
curl_setopt($this->_connection, CURLOPT_PROGRESSFUNCTION, function(
$downloadSize, $downloaded, $uploadSize, $uploaded
){
return ($downloaded > CRAWL_CURLOPT_PROGRESSFUNCTION_DOWNLOAD_SIZE_LIMIT) ? 1 : 0;
});
2023-04-01 16:29:39 +00:00
if ($userAgent) {
curl_setopt($this->_connection, CURLOPT_USERAGENT, (string) $userAgent);
}
2023-04-03 01:47:31 +00:00
$this->_response = curl_exec($this->_connection);
2023-04-01 16:29:39 +00:00
}
public function __destruct() {
curl_close($this->_connection);
}
public function getError() {
if (curl_errno($this->_connection)) {
return curl_errno($this->_connection);
} else {
return false;
}
}
public function getCode() {
return curl_getinfo($this->_connection, CURLINFO_HTTP_CODE);
}
public function getContentType() {
return curl_getinfo($this->_connection, CURLINFO_CONTENT_TYPE);
}
2023-05-08 05:27:21 +00:00
public function getSizeDownload() {
return curl_getinfo($this->_connection, CURLINFO_SIZE_DOWNLOAD);
}
public function getSizeRequest() {
return curl_getinfo($this->_connection, CURLINFO_REQUEST_SIZE);
}
public function getTotalTime() {
return curl_getinfo($this->_connection, CURLINFO_TOTAL_TIME_T);
}
2023-04-01 16:29:39 +00:00
public function getContent() {
2023-04-03 01:47:31 +00:00
return $this->_response;
2023-04-01 16:29:39 +00:00
}
}