Cixo Develop 4 сар өмнө
parent
commit
ebe570dfa9
2 өөрчлөгдсөн 151 нэмэгдсэн , 0 устгасан
  1. 68 0
      sources/fetch.php
  2. 83 0
      sources/response.php

+ 68 - 0
sources/fetch.php

@@ -0,0 +1,68 @@
+<?php
+
+require('response.php');
+
+class fetch {
+    private bool $has_content;
+    private string $method;
+    private ?string $received;
+    private CurlHandle $request;
+
+    public function __construct(string $url, string $method = 'GET') {
+        $this->received = null;
+        $this->has_content = false;
+        $this->method = $method;
+
+        $this->request = curl_init();
+        $this->setopt(CURLOPT_RETURNTRANSFER, true);
+        $this->setopt(CURLOPT_FAILONERROR, true);
+        $this->setopt(CURLOPT_HEADER, true);
+        $this->setopt(CURLOPT_CUSTOMREQUEST, $method);
+        $this->setopt(CURLOPT_URL, curl_escape($url));
+    }
+
+    public function set_json(array $content): object {
+        if ($this->has_content) {
+            throw new RuntimeException('JSON content already set.');
+        }
+
+        if ($this->method === 'GET' or $this->method === 'HEAD') {
+            throw new TypeError("JSON request can not be GET or HEAD.");
+        }
+
+        $converted = json_encode($content);
+        $headers = [ 'Content-Type: application/json' ];
+        
+        $this->has_content = true;
+        $this->setopt(CURLOPT_POSTFIELDS, $converted);
+        $this->setopt(CURLOPT_HTTPHEADER, $headers);
+
+        return $this;
+    }
+
+    public function request(): string { 
+        if ($this->received !== null) {
+            return $this->received;
+        }
+
+        $result = curl_exec($this->request);
+        $error = curl_error($this->request);
+        $error_number = curl_errno($this->request);
+        
+        curl_close($this->received);
+        
+        if ($result !== false and $error_number === 0) {
+            return $this->received = $result;
+        }
+
+        throw new RuntimeException(
+            'Can not fetch request. Error code '
+            .string($error_number)
+            .': "'.$error.'".'
+        );
+    }
+
+    private function setopt(int $option, mixed $content): void {
+        curl_setopt($this->request, $option, $content);
+    }
+}

+ 83 - 0
sources/response.php

@@ -0,0 +1,83 @@
+<?php
+
+class response {
+    private int $code;
+    private array $parsed;
+    private array $headers;
+    private string $version;
+    private string $content;
+
+    public function __construct(string $content) {
+        $parts = explode("\r\n\r\n", $content);
+        $head = array_shift($parts);
+        $body = join("\r\n\r\n", $parts);
+
+        $this->parse_head($head);
+        $this->parse_body($body);
+    }
+
+    public function get_code(): int {
+        return $this->code;
+    }
+
+    public function get_version(): string {
+        return $this->version;
+    }
+
+    public function get_header(
+        string $name, 
+        ?string $fallback = null
+    ): ?string {
+        if (array_key_exists($name, $this->headers)) {
+            return $this->headers[$name];
+        }
+
+        return $fallback;
+    }
+
+    public function get_content(): string {
+        return $this->content;
+    }
+
+    public function get_json_content(): array {
+        return $this->parsed;
+    }
+
+    private function parse_body(string $body): void {
+        $type = $this->get_header('Content-Type', 'text/plain'));
+        $is_json = strpos($type, 'json') !== -1;
+
+        $this->content = $body;        
+        $this->parsed = array();
+
+        if ($is_json) {
+            $this->parsed = json_decode($body);
+        }
+    }
+
+    private function parse_head(string $head): void {
+        $lines = explode("\r\n", $head);
+        $first = array_shift($lines);
+        
+        $this->parse_first_line($first);
+        $this->parse_headers($lines);
+    }
+
+    private function parse_first_line(string $line): void {
+        $parts = explode(' ', $line);
+        $this->version = array_shift($parts);
+        $this->code = int(array_shift($parts));
+    }
+
+    private function parse_headers(array $lines): void {
+        $this->headers = array();
+
+        foreach ($lines as $line) {
+            $parts = explode(':', $line);
+            $name = array_shift($parts);
+            $content = ltrim(join(':', $parts));
+
+            $this->headers[$name] = $content;
+        }
+    }
+}