|
|
@@ -54,6 +54,12 @@ class fetch {
|
|
|
*/
|
|
|
private string $url;
|
|
|
|
|
|
+ /**
|
|
|
+ * This store headers of the request.
|
|
|
+ * @var array
|
|
|
+ */
|
|
|
+ private array $headers;
|
|
|
+
|
|
|
/**
|
|
|
* This create new fetch request. It is alias of constructor.
|
|
|
*
|
|
|
@@ -77,6 +83,7 @@ class fetch {
|
|
|
$this->content = null;
|
|
|
$this->content_type = null;
|
|
|
$this->method = 'GET';
|
|
|
+ $this->headers = array();
|
|
|
$this->url = $url;
|
|
|
}
|
|
|
|
|
|
@@ -118,6 +125,55 @@ class fetch {
|
|
|
return $this;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * That add new header to the request. Name of the header must be full
|
|
|
+ * featured header name, like 'Cache-Control'. It also had been validated
|
|
|
+ * and when name had been blacklisted, it raise RuntimeException. It also
|
|
|
+ * raise RuntimeException when name has any white chars. Content had been
|
|
|
+ * validating, and it could not have new lines. Name and content would be
|
|
|
+ * trimed before processing.
|
|
|
+ *
|
|
|
+ * Blacklisted headers:
|
|
|
+ * Content-Type, Content-Length
|
|
|
+ *
|
|
|
+ * @param string $name Name of the header.
|
|
|
+ * @param string $content Content of the header.
|
|
|
+ * @return fetch Self to chain processing.
|
|
|
+ */
|
|
|
+ public function add_header(string $name, string $content): object {
|
|
|
+ $name = trim($name);
|
|
|
+ $content = trim($content);
|
|
|
+
|
|
|
+ if (strpos($name, ' ') !== false) {
|
|
|
+ throw new RuntimeException(
|
|
|
+ 'Header "'.$name.'" contains white char.'
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ $check_content = strpos($content, ' ') !== false;
|
|
|
+ $check_content = $check_content || strpos($content, "\r") !== false;
|
|
|
+ $check_content = $check_content || strpos($content, "\n") !== false;
|
|
|
+
|
|
|
+ if ($check_content) {
|
|
|
+ throw new RuntimeException(
|
|
|
+ 'Header content "'.$content.'" contains invalid white char.'
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($name === 'Content-Type') {
|
|
|
+ throw new RuntimeException('Content-Type header is automatic.');
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($name === 'Content-Lenght') {
|
|
|
+ throw new RuntimeException('Content-Lenght header is automatic.');
|
|
|
+ }
|
|
|
+
|
|
|
+ $header = $name.': '.$content;
|
|
|
+ array_push($this->headers, $header);
|
|
|
+
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* This function set method of the request. Be careful, because it check
|
|
|
* that any content is not set, and when content is not null, but trying
|
|
|
@@ -148,21 +204,22 @@ class fetch {
|
|
|
*
|
|
|
* @return response Response from the server.
|
|
|
*/
|
|
|
- public function request(): response {
|
|
|
+ public function request(): response {
|
|
|
+ $headers = $this->headers;
|
|
|
$request = curl_init($this->url);
|
|
|
+
|
|
|
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
|
|
|
curl_setopt($request, CURLOPT_FAILONERROR, true);
|
|
|
curl_setopt($request, CURLOPT_HEADER, true);
|
|
|
curl_setopt($request,CURLOPT_CUSTOMREQUEST, $this->method);
|
|
|
|
|
|
if ($this->content !== null) {
|
|
|
- $type = 'Content-Type: '.$this->content_type;
|
|
|
- $headers = [ $type ];
|
|
|
-
|
|
|
- curl_setopt($request, CURLOPT_HTTPHEADER, $headers);
|
|
|
+ array_push($headers, 'Content-Type: '.$this->content_type);
|
|
|
curl_setopt($request, CURLOPT_POSTFIELDS, $this->content);
|
|
|
}
|
|
|
|
|
|
+ curl_setopt($request, CURLOPT_HTTPHEADER, $headers);
|
|
|
+
|
|
|
$result = curl_exec($request);
|
|
|
$error = curl_error($request);
|
|
|
$error_number = curl_errno($request);
|
|
|
@@ -179,5 +236,5 @@ class fetch {
|
|
|
.': "'.$error.'".'
|
|
|
);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|