├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── docs ├── README.md ├── _config.yml ├── base-url.md ├── custom-headers-and-the-user-agent.md ├── following-redirects.md ├── installation.md ├── making-a-request.md ├── parameter-bags.md └── using-responses.md ├── example.php ├── phpunit.xml ├── src ├── AbstractRequest.php ├── Client.php ├── HttpRequestException.php ├── JsonConversionException.php ├── ParameterBag.php ├── Request.php └── Response.php └── tests └── http ├── RequestTest.php └── ResponseTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | ._* 3 | /vendor 4 | /composer.lock 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | php: 3 | - 7.3 4 | - 7.4 5 | before_script: 6 | - composer install 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright © 2020 Maximilian Götz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the “Software”), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SimplecURL 2 | 3 | **This project is archived! Use at your own caution.** 4 | 5 | --- 6 | 7 | [![Build Status](https://travis-ci.org/ImMaax/SimplecURL.svg?branch=master)](https://travis-ci.org/ImMaax/SimplecURL) 8 | [![Code Climate maintainability](https://img.shields.io/codeclimate/maintainability-percentage/ImMaax/SimplecURL)](https://codeclimate.com/github/ImMaax/SimplecURL) 9 | 10 | SimplecURL is a simple and modern wrapper around PHP's [`ext-curl`](https://www.php.net/manual/en/book.curl.php). 11 | 12 | ```php 13 | $client = new SimplecURL\Client; 14 | $res = $client->request('GET', 'http://127.0.0.1:8080/'); 15 | 16 | echo $res->getBody(); 17 | ``` 18 | 19 | ## Installation 20 | 21 | ```sh 22 | composer require simplecurl/simplecurl 23 | ``` 24 | 25 | ## Docs 26 | 27 | Docs can be found in `/docs` or at [https://immaax.github.io/SimplecURL/](https://immaax.github.io/SimplecURL/). 28 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "simplecurl/simplecurl", 3 | "description": "Easy HTTP requests", 4 | "type": "library", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Maximilian Götz", 9 | "email": "contact@maxbits.net", 10 | "homepage": "https://www.maxbits.net/" 11 | } 12 | ], 13 | "require": { 14 | "ext-curl": "*" 15 | }, 16 | "autoload": { 17 | "psr-4": { 18 | "SimplecURL\\": "src/" 19 | } 20 | }, 21 | "require-dev": { 22 | "phpstan/phpstan": "^0.12.18", 23 | "phpunit/phpunit": "^9.1" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | # SimplecURL Documentation 2 | 3 | **This project is archived. Use at your own caution.** 4 | 5 | --- 6 | 7 | Welcome to the SimplecURL docs! SimplecURL is an object-oriented wrapper around PHP's `ext-curl`. It allows for easy and quick HTTP requests. 8 | 9 | ```php 10 | $client = new SimplecURL\Client; 11 | $res = $client->request('GET', 'http://127.0.0.1:8080/'); 12 | 13 | echo $res->getBody(); 14 | ``` 15 | 16 | ## Table of Contents 17 | 18 | - [Installation](installation.md) 19 | - [Making a Request](making-a-request.md) 20 | - [Parameter Bags](parameter-bags.md) 21 | - [Using Responses](using-responses.md) 22 | - [Custom Headers and the User-Agent](custom-headers-and-the-user-agent.md) 23 | - [Following Redirects](following-redirects.md) 24 | - [Base URL](base-url.md) 25 | -------------------------------------------------------------------------------- /docs/_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-slate -------------------------------------------------------------------------------- /docs/base-url.md: -------------------------------------------------------------------------------- 1 | # SimplecURL - Base URL 2 | 3 | **This project is archived. Use at your own caution.** 4 | 5 | --- 6 | 7 | You can set a base URL for every request: 8 | 9 | ```php 10 | $client = new SimplecURL\Client('https://httpbin.org'); 11 | $client->request('GET', '/get'); # Makes a request to https://httpbin.org/get 12 | $client->request('POST', '/post'); # Makes a request to https://httpbin.org/post 13 | ``` 14 | 15 | This might be helpful if you create a client for use with a single server or with a specific directory, as you don't need to specify the base URL over and over again in each request. 16 | -------------------------------------------------------------------------------- /docs/custom-headers-and-the-user-agent.md: -------------------------------------------------------------------------------- 1 | # SimplecURL - Custom Headers and the User Agent 2 | 3 | **This project is archived. Use at your own caution.** 4 | 5 | --- 6 | 7 | ## Changing the User Agent 8 | 9 | Under some circumstances you may need to change the library's user agent (the default user agent looks like this: `SimplecURL/0.1.0 (like cURL/7.67.0; PHP/7.4.1)`): 10 | 11 | ```php 12 | $client = new SimplecURL\Client; 13 | $client->setUseragent('CustomUA/1.0.0'); 14 | ``` 15 | 16 | The above code snippet sets the client's user agent to `CustomUA/1.0.0`. It will be applied on all requests made using this client. 17 | 18 | ## Adding custom Headers 19 | 20 | Attaching custom headers to a request is very easy: 21 | 22 | ```php 23 | $bag = new SimplecURL\ParameterBag; 24 | $bag->setHeader('Name', 'Value'); 25 | 26 | $res = $client->request('GET', 'http://127.0.0.1:8080/', $bag); 27 | 28 | # +----+ 29 | # | Or | 30 | # +----+ 31 | 32 | $res = $client->request('GET', 'http://127.0.0.1:8080/', [ 33 | 'headers' => [ 34 | 'Name: Value' 35 | ] 36 | ]); 37 | ``` 38 | 39 | This will add the header `Name` with a value of `Value`. 40 | 41 | *Next: [Following Redirects](following-redirects.md)* 42 | -------------------------------------------------------------------------------- /docs/following-redirects.md: -------------------------------------------------------------------------------- 1 | # SimplecURL - Following Redirects 2 | 3 | **This project is archived. Use at your own caution.** 4 | 5 | --- 6 | 7 | By default, SimplecURL follows all redirects. However, you can either completely disable that behavior or limit it to a given number of redirects: 8 | 9 | ```php 10 | $bag = new SimplecURL\ParameterBag; 11 | $bag->allowRedirects(); 12 | $bag->setMaxRedirects(5); 13 | 14 | $res = $client->request('POST', 'http://127.0.0.1:8080/', $bag); 15 | 16 | # +----+ 17 | # | Or | 18 | # +----+ 19 | 20 | $res = $client->request('POST', 'http://127.0.0.1:8080/', [ 21 | 'redirects' => [ 22 | 'allow' => true, 23 | 'maxRedirects' => 5 24 | ] 25 | ]); 26 | ``` 27 | 28 | The above example allows up to 5 redirects. 29 | 30 | ```php 31 | $bag = new SimplecURL\ParameterBag; 32 | $bag->disallowRedirects(); 33 | 34 | $res = $client->request('POST', 'http://127.0.0.1:8080/', $bag); 35 | 36 | # +----+ 37 | # | Or | 38 | # +----+ 39 | 40 | $res = $client->request('POST', 'http://127.0.0.1:8080/', [ 41 | 'redirects' => [ 42 | 'allow' => false 43 | ] 44 | ]); 45 | ``` 46 | 47 | This example disables redirects completely. 48 | 49 | *[Next: Base URL](base-url.md)* 50 | -------------------------------------------------------------------------------- /docs/installation.md: -------------------------------------------------------------------------------- 1 | # SimplecURL - Installation 2 | 3 | **This project is archived. Use at your own caution.** 4 | 5 | --- 6 | 7 | SimplecURL is installed using [Composer](https://getcomposer.org/): 8 | 9 | ```sh 10 | composer require simplecurl/simplecurl 11 | ``` 12 | 13 | After installing it, `require` Composer's autoloader: 14 | 15 | ```php 16 | require 'vendor/autoload.php'; 17 | ``` 18 | 19 | *Next: [Making a Request](making-a-request.md)* 20 | -------------------------------------------------------------------------------- /docs/making-a-request.md: -------------------------------------------------------------------------------- 1 | # SimplecURL - Making a Request 2 | 3 | **This project is archived. Use at your own caution.** 4 | 5 | --- 6 | 7 | ## Creating a new Client 8 | 9 | To make requests, you need to create a client: 10 | 11 | ```php 12 | $client = new SimplecURL\Client; 13 | ``` 14 | 15 | ## Sending a Request 16 | 17 | ```php 18 | $res = $client->request('GET', 'http://127.0.0.1:8080/'); 19 | ``` 20 | 21 | The above code snippet sends a `GET` request to `http://127.0.0.1:8080/`. 22 | 23 | You can also use other methods, such as `DELETE`, to make your requests: 24 | 25 | ```php 26 | $res = $client->request('DELETE', 'http://127.0.0.1:8080/'); 27 | ``` 28 | 29 | ## Sending `POST` Data 30 | 31 | If you want to send some form data using a `POST` request, pass it to `request()`'s options array: 32 | 33 | ```php 34 | $res = $client->request('POST', 'http://127.0.0.1:8080/', [ 35 | 'postfields' => [ 36 | 'Foo' => 'Bar' 37 | ] 38 | ]); 39 | ``` 40 | 41 | This will add the `POST` parameter `Foo` with a value of `Bar` to your request. 42 | 43 | *Next: [Parameter Bags](parameter-bags.md)* 44 | -------------------------------------------------------------------------------- /docs/parameter-bags.md: -------------------------------------------------------------------------------- 1 | # SimplecURL - Parameter Bags 2 | 3 | **This project is archived. Use at your own caution.** 4 | 5 | --- 6 | 7 | ## What are they and how do they work? 8 | 9 | Look at the following request example: 10 | 11 | ```php 12 | $res = $client->request('POST', 'http://127.0.0.1:8080/', [ 13 | 'postfields' => [ 14 | 'Foo' => 'Bar' 15 | ] 16 | ]); 17 | ``` 18 | 19 | The above snippet uses an array for additional options. 20 | However, you can also use objects to set options: 21 | 22 | ```php 23 | $bag = new SimplecURL\ParameterBag; 24 | $bag->setPostfield('Foo', 'Bar'); 25 | 26 | $res = $client->request('POST', 'http://127.0.0.1:8080/', $bag); 27 | ``` 28 | 29 | Both code snippets will produce the exact same result. 30 | Everything that can be set using arrays can also be set using a parameter bag: 31 | 32 | ```php 33 | $bag = new SimplecURL\ParameterBag; 34 | 35 | $bag->setHeader('Some-Header', 'True'); 36 | $bag->setPostfield('Foo', 'Bar'); 37 | $bag->allowRedirects(); /* and */ $bag->disallowRedirects(); 38 | $bag->setMaxRedirects(10); 39 | ``` 40 | 41 | Those values can also be retrieved from the bag: 42 | 43 | ```php 44 | $bag->getHeaders(); 45 | $bag->getPostfields(); 46 | $bag->getPostfield('SomeField'); 47 | $bag->getRedirectsAllowed(); 48 | $bag->getMaxRedirects(); 49 | ``` 50 | 51 | ## Should I use arrays or parameter bags? 52 | 53 | That's totally up to you, it's just personal preference. 54 | This manual will show both ways of doing it. 55 | 56 | *Next: [Using Responses](using-responses.md)* 57 | -------------------------------------------------------------------------------- /docs/using-responses.md: -------------------------------------------------------------------------------- 1 | # SimplecURL - Using Responses 2 | 3 | **This project is archived. Use at your own caution.** 4 | 5 | --- 6 | 7 | Say you have a request like this: 8 | 9 | ```php 10 | $res = $client->request('GET', 'http://127.0.0.1:8080/'); 11 | ``` 12 | 13 | ## Getting the Body 14 | 15 | You can get the response body using `$res->getBody()`: 16 | 17 | ```php 18 | $res = $client->request('GET', 'http://127.0.0.1:8080/'); 19 | 20 | echo $res->getBody(); 21 | ``` 22 | 23 | ### JSON 24 | 25 | To convert a JSON response to an object, use `json()`: 26 | 27 | ```php 28 | $res = $client->request('GET', 'http://127.0.0.1:8080/'); 29 | print_r($res->json()); 30 | # +----+ 31 | # | Or | 32 | # +----+ 33 | $res = $client->request('GET', 'http://127.0.0.1:8080/')->json(); 34 | print_r($res); 35 | ``` 36 | 37 | ## Getting Headers 38 | 39 | There are multiple ways to get all or one specific header: 40 | 41 | ```php 42 | $res = $client->request('GET', 'http://127.0.0.1:8080/'); 43 | 44 | print_r($res->getHeaders()); # => Prints an array of all headers and their values: ['Header' => [ 0 => 'Value']] 45 | print_r($res->getHeader('name')); # => Prints an array with the value of the header "name": [0 => 'Value'] 46 | ``` 47 | 48 | ## Getting the Status Code 49 | 50 | You can get the status code using `getStatusCode`: 51 | 52 | ```php 53 | $res = $client->request('GET', 'http://127.0.0.1:8080/'); 54 | 55 | echo $res->getStatusCode(); 56 | ``` 57 | 58 | It is a 3 digit integer. 59 | 60 | *Next: [Custom Headers and the User-Agent](custom-headers-and-the-user-agent.md)* 61 | -------------------------------------------------------------------------------- /example.php: -------------------------------------------------------------------------------- 1 | setUseragent('CustomUA/1.0.0'); 6 | 7 | # Object version: 8 | $bag = new SimplecURL\ParameterBag; 9 | $bag->setHeader('X-Some-Header', 'Value'); 10 | $bag->setPostfield('Foo', 'Bar'); 11 | $bag->allowRedirects(); 12 | $bag->setMaxRedirects(5); 13 | 14 | $res = $client->request('POST', 'http://127.0.0.1:8080/', $bag); 15 | 16 | # Array version: 17 | $res = $client->request('POST', 'http://127.0.0.1:8080/', [ 18 | 'headers' => [ 19 | 'X-Some-Header: Value' 20 | ], 21 | 'postfields' => [ 22 | 'Foo' => 'Bar' 23 | ], 24 | 'redirects' => [ 25 | 'allow' => true, 26 | 'maxRedirects' => 5 27 | ] 28 | ]); 29 | 30 | print_r($res->getHeaders()); 31 | echo $res->getBody(); 32 | echo $res->getStatusCode(); 33 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | tests 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/AbstractRequest.php: -------------------------------------------------------------------------------- 1 | method = $method; 22 | } 23 | 24 | public function setUrl(string $url): void { 25 | $this->url = $url; 26 | } 27 | 28 | public function setUseragent(string $useragent): void { 29 | $this->useragent = $useragent; 30 | } 31 | 32 | public function setHeaders(array $headers): void { 33 | $this->headers = $headers; 34 | } 35 | 36 | public function setPostfields(string $fields): void { 37 | $this->postfields = $fields; 38 | } 39 | 40 | public function disallowRedirects(): void { 41 | $this->allowRedirects = false; 42 | } 43 | 44 | public function allowRedirects(): void { 45 | $this->allowRedirects = true; 46 | } 47 | 48 | public function maxRedirects(int $maxRedirects): void { 49 | $this->maxRedirects = $maxRedirects; 50 | } 51 | 52 | abstract public function send(): Response; 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/Client.php: -------------------------------------------------------------------------------- 1 | setUseragent($this->defaultUseragent()); 14 | $this->baseurl = $baseurl; 15 | } 16 | 17 | public function setUseragent(string $useragent): void { 18 | $this->useragent = $useragent; 19 | } 20 | 21 | public function getUseragent(): string { 22 | return $this->useragent; 23 | } 24 | 25 | public function request(string $method, string $url, $options = []): Response { 26 | switch (gettype($options)) { 27 | case 'array': 28 | # Everything stays the same 29 | break; 30 | case 'object': 31 | if (get_class($options) != 'SimplecURL\\ParameterBag') { 32 | throw new \InvalidArgumentException('The options passed to a new request must be either an array or a ParameterBag, got ' . get_class($options)); 33 | } 34 | 35 | $parameters = $options; 36 | $options = [ 37 | 'redirects' => [] 38 | ]; 39 | 40 | $options['headers'] = $parameters->getHeaders(); 41 | $options['postfields'] = $parameters->getPostfields(); 42 | $options['redirects']['allow'] = $parameters->getRedirectsAllowed(); 43 | $options['redirects']['max'] = $parameters->getMaxRedirects(); 44 | break; 45 | default: 46 | throw new \InvalidArgumentException('The options passed to a new request must be either an array or a ParameterBag, got ' . gettype($options)); 47 | } 48 | 49 | $req = new Request(); 50 | 51 | $req->allowRedirects(); 52 | $req->setMethod($method); 53 | $req->setUrl($this->baseurl . $url); 54 | $req->setUseragent($this->getUseragent()); 55 | 56 | if (isset($options['headers'])) { 57 | $req->setHeaders($options['headers']); 58 | } 59 | 60 | if (isset($options['postfields'])) { 61 | $req->setPostfields(http_build_query($options['postfields'])); 62 | } 63 | 64 | if (isset($options['redirects'])) { 65 | if (isset($options['redirects']['allow']) && $options['redirects']['allow'] == false) { 66 | $req->disallowRedirects(); 67 | } 68 | 69 | if (isset($options['redirects']['max'])) { 70 | $req->maxRedirects($options['redirects']['max']); 71 | } else { 72 | $req->maxRedirects(10); 73 | } 74 | } 75 | 76 | return $req->send(); 77 | } 78 | 79 | protected function defaultUseragent(): string { 80 | return 'SimplecURL/' . self::VERSION . ' (like cURL/' . curl_version()['version'] . '; PHP/' . phpversion() . ')'; 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /src/HttpRequestException.php: -------------------------------------------------------------------------------- 1 | headers = $headers; 16 | } 17 | 18 | public function getHeaders(): array { 19 | return $this->headers; 20 | } 21 | 22 | public function setHeader(string $name, string $value): void { 23 | $this->headers[] = $name . ': ' . $value; 24 | } 25 | 26 | public function setPostfields(array $postfields): void { 27 | $this->postfields = $postfields; 28 | } 29 | 30 | public function getPostfields(): array { 31 | return $this->postfields; 32 | } 33 | 34 | public function setPostfield(string $name, string $value): void { 35 | $this->postfields[$name] = $value; 36 | } 37 | 38 | public function getPostfield(string $name): string { 39 | return $this->postfields[$name]; 40 | } 41 | 42 | public function allowRedirects(): void { 43 | $this->allow_redirects = true; 44 | } 45 | 46 | public function disallowRedirects(): void { 47 | $this->allow_redirects = false; 48 | } 49 | 50 | public function getRedirectsAllowed(): bool { 51 | return $this->allow_redirects; 52 | } 53 | 54 | public function setMaxRedirects(int $max_redirects): void { 55 | $this->max_redirects = $max_redirects; 56 | } 57 | 58 | public function getMaxRedirects(): int { 59 | return $this->max_redirects; 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/Request.php: -------------------------------------------------------------------------------- 1 | allowRedirects); 12 | curl_setopt($ch, CURLOPT_URL, $this->url); 13 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 14 | curl_setopt($ch, CURLOPT_USERAGENT, $this->useragent); 15 | /** @see https://stackoverflow.com/a/41135574/9681396 */ 16 | curl_setopt($ch, CURLOPT_HEADERFUNCTION, 17 | function($curl, $header) use (&$headers) { 18 | $len = strlen($header); 19 | $header = explode(':', $header, 2); 20 | if (count($header) < 2) return $len; 21 | 22 | $headers[strtolower(trim($header[0]))][] = trim($header[1]); 23 | 24 | return $len; 25 | } 26 | ); 27 | 28 | if (!empty($this->headers)) { 29 | curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers); 30 | } 31 | 32 | if (!empty($this->maxRedirects)) { 33 | curl_setopt($ch, CURLOPT_MAXREDIRS, $this->maxRedirects); 34 | } 35 | 36 | switch($this->method) { 37 | case 'GET': curl_setopt($ch, CURLOPT_HTTPGET, 1); break; 38 | case 'POST': curl_setopt($ch, CURLOPT_POST, 1); 39 | if (!empty($this->postfields)) curl_setopt($ch, CURLOPT_POSTFIELDS, $this->postfields); 40 | break; 41 | default: curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method); 42 | } 43 | 44 | if (!$body = curl_exec($ch)) { 45 | throw new HttpRequestException(curl_errno($ch) . ' - ' . curl_error($ch)); 46 | } 47 | 48 | $res->setStatusCode(curl_getinfo($ch, CURLINFO_HTTP_CODE)); 49 | $res->setHeaders($headers); 50 | $res->setBody($body); 51 | 52 | curl_close($ch); 53 | 54 | return $res; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/Response.php: -------------------------------------------------------------------------------- 1 | headers; 14 | } 15 | 16 | public function setHeaders(array $headers): void { 17 | $this->headers = $headers; 18 | } 19 | 20 | public function getHeader(string $name) { 21 | if ($this->doesHeaderExist($name)) { 22 | return $this->getHeaders()[$name]; 23 | } else { 24 | return null; 25 | } 26 | } 27 | 28 | public function setHeader(string $name, string $value): void { 29 | $this->headers[$name] = [ 30 | 0 => $value 31 | ]; 32 | } 33 | 34 | public function doesHeaderExist(string $name): bool { 35 | return isset($this->getHeaders()[$name]); 36 | } 37 | 38 | public function getBody(): string { 39 | return $this->body; 40 | } 41 | 42 | public function setBody(string $body): void { 43 | $this->body = $body; 44 | } 45 | 46 | public function setStatusCode(int $status): void { 47 | $this->statusCode = $status; 48 | } 49 | 50 | public function getStatusCode(): int { 51 | return $this->statusCode; 52 | } 53 | 54 | /** 55 | * @return object|array 56 | */ 57 | public function json() { 58 | $json = json_decode($this->getBody()); 59 | 60 | if ($json === null && json_last_error() !== JSON_ERROR_NONE) { 61 | throw new JsonConversionException('Invalid JSON data supplied!'); 62 | } 63 | 64 | return $json; 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /tests/http/RequestTest.php: -------------------------------------------------------------------------------- 1 | client = new SimplecURL\Client('https://httpbin.org'); 10 | } 11 | 12 | public function testClient(): void { 13 | $this->assertInstanceOf(SimplecURL\Client::class, $this->client); 14 | } 15 | 16 | public function testGet(): void { 17 | $res = $this->client->request('GET', '/get'); 18 | $this->assertInstanceOf(SimplecURL\Response::class, $res); 19 | } 20 | 21 | public function testPost(): void { 22 | $res = $this->client->request('POST', '/post', [ 23 | 'postfields' => [ 24 | 'Unit' => 'Test' 25 | ] 26 | ]); 27 | $this->assertInstanceOf(SimplecURL\Response::class, $res); 28 | 29 | $json = $res->json(); 30 | $this->assertObjectHasAttribute('Unit', $json->form); 31 | } 32 | 33 | public function testPostParameterBag(): void { 34 | $bag = new SimplecURL\ParameterBag; 35 | $bag->setPostfield('Unit', 'Test'); 36 | 37 | $res = $this->client->request('POST', '/post', $bag); 38 | $this->assertInstanceOf(SimplecURL\Response::class, $res); 39 | 40 | $json = $res->json(); 41 | $this->assertObjectHasAttribute('Unit', $json->form); 42 | } 43 | 44 | public function testHeaders(): void { 45 | $res = $this->client->request('GET', '/headers', [ 46 | 'headers' => [ 47 | 'X-Unit-Test: True' 48 | ] 49 | ])->json()->headers; 50 | 51 | $this->assertObjectHasAttribute('X-Unit-Test', $res); 52 | } 53 | 54 | public function testHeadersParameterBag(): void { 55 | $bag = new SimplecURL\ParameterBag; 56 | $bag->setHeader('X-Unit-Test', 'True'); 57 | 58 | $res = $this->client->request('GET', '/headers', $bag)->json()->headers; 59 | 60 | $this->assertObjectHasAttribute('X-Unit-Test', $res); 61 | } 62 | 63 | public function testRedirectsAllowed(): void { 64 | try { 65 | $res = $this->client->request('GET', '/headers', [ 66 | 'redirects' => [ 67 | 'allow' => true 68 | ] 69 | ]); 70 | 71 | $this->assertInstanceOf(SimplecURL\Response::class, $res); 72 | } catch(SimplecURL\HttpRequestException $e) { 73 | $this->fail('Redirects were allowed, but the client didn\'t follow the redirect.'); 74 | } 75 | } 76 | 77 | public function testRedirectsAllowedParameterBag(): void { 78 | try { 79 | $bag = new SimplecURL\ParameterBag; 80 | $bag->allowRedirects(); 81 | 82 | $res = $this->client->request('GET', '/headers', $bag); 83 | 84 | $this->assertInstanceOf(SimplecURL\Response::class, $res); 85 | } catch(SimplecURL\HttpRequestException $e) { 86 | $this->fail('Redirects were allowed, but the client didn\'t follow the redirect.'); 87 | } 88 | } 89 | 90 | public function testUseragent(): void { 91 | $this->client->setUseragent('UnitTestRequest/0.1.0'); 92 | $res = $this->client->request('GET', '/user-agent')->json()->{'user-agent'}; 93 | 94 | $this->assertEquals('UnitTestRequest/0.1.0', $res); 95 | } 96 | 97 | } 98 | -------------------------------------------------------------------------------- /tests/http/ResponseTest.php: -------------------------------------------------------------------------------- 1 | res = (new SimplecURL\Client)->request('GET', 'https://httpbin.org/json'); 10 | } 11 | 12 | public function testBody(): void { 13 | $this->assertNotEmpty($this->res->getBody()); 14 | } 15 | 16 | public function testHeaders(): void { 17 | $this->assertNotEmpty($this->res->getHeaders()); 18 | } 19 | 20 | public function testHeader(): void { 21 | $this->assertNotEmpty($this->res->getHeader('date')[0]); 22 | } 23 | 24 | public function testStatusCode(): void { 25 | $this->assertIsInt($this->res->getStatusCode()); 26 | } 27 | 28 | public function testJsonConversion(): void { 29 | $this->assertIsObject($this->res->json()); 30 | } 31 | 32 | } 33 | --------------------------------------------------------------------------------