Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/byjg/php-webrequest
A lightweight and highly customized CURL wrapper for making RESt calls and a wrapper for call dynamically SOAP requests.
https://github.com/byjg/php-webrequest
curl hacktoberfest php
Last synced: 4 days ago
JSON representation
A lightweight and highly customized CURL wrapper for making RESt calls and a wrapper for call dynamically SOAP requests.
- Host: GitHub
- URL: https://github.com/byjg/php-webrequest
- Owner: byjg
- License: mit
- Created: 2015-07-14T03:58:25.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-09-16T14:44:01.000Z (about 2 months ago)
- Last Synced: 2024-10-19T03:04:42.840Z (18 days ago)
- Topics: curl, hacktoberfest, php
- Language: PHP
- Homepage:
- Size: 188 KB
- Stars: 5
- Watchers: 3
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Web Request
[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg-success.svg)](http://opensource.byjg.com)
[![Build Status](https://github.com/byjg/webrequest/actions/workflows/phpunit.yml/badge.svg?branch=master)](https://github.com/byjg/webrequest/actions/workflows/phpunit.yml)
[![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/webrequest/)
[![GitHub license](https://img.shields.io/github/license/byjg/webrequest.svg)](https://opensource.byjg.com/opensource/licensing.html)
[![GitHub release](https://img.shields.io/github/release/byjg/webrequest.svg)](https://github.com/byjg/webrequest/releases/)A lightweight PSR-7 implementation and and highly customized CURL wrapper for making RESt calls.
## Main Features
This class implements:
* PSR-7 Request and Response;
* PSR-18 Http Client
* Helper to create Request instances with the most common use cases;
* Wrapper to execute several requests in parallel;## PSR-7 Implementation and basic usage
Since the implementation follow the PSR7 implementation there is no much explanation about the usage.
The key elements are:
* URI - Will define the URI with parameters, path, host, schema, etc
* Request - Will set the request headers and method;
* Response - Will receive the response header, body and status code.More information about the PSR-7 [here](https://www.php-fig.org/psr/psr-7/).
The implementation to send the request object is defined by the class `HttpClient`.
This class follow partially the PSR-18 implementation.
So, once you have a Request instance defined just need to call `HttpClient::sendRequest($request);`### Basic Usage
```php
sendRequest($request);
```### Passing arguments
```php
withQuery(http_build_query(['param'=>'value']));$request = \ByJG\WebRequest\Psr7\Request::getInstance($uri);
$response = \ByJG\WebRequest\HttpClient::getInstance()->sendRequest($request);
```## Helper Classes
The WebRequest package has Helper classes to make it easy to create Request instances for some use cases.
### Passing a string payload (JSON)
```php
sendRequest($request);
```### Create a Form Url Encoded (emulate HTTP form)
```php
"value"]
);
$response = \ByJG\WebRequest\HttpClient::getInstance()->sendRequest($request);
```### Create a Multi Part request (upload documents)
```php
sendRequest($request);
```## Customizing the Http Client
The customizations options are:
```php
withNoFollowRedirect() // HttpClient will not follow redirects (status codes 301 and 302). Default is follow
->withNoSSLVerification() // HttpClient will not validate the SSL certificate. Default is validate.
->withProxy($uri) // Define a http Proxy based on the URI.
->withCurlOption($key, $value) // Set an arbitrary CURL option (use with caution)
;```
## HttpClientParallel
You can use the HttpClient to do several differents requests in parallel.
To use this funcionallity you need:
1. Create a instance of the HttpClientParallel class
2. Add the RequestInterface instance
3. ExecuteThe results will be processed as soon is ready.
Below a basic example:
```php
addRequest($request1)
->addRequest($request2)
->addRequest($request3);// Start execute and wait to finish
// The results will be get from the closure defined above.
$multi->execute();
```## Mocking Http Client
The class `MockClient` has the same methods that HttpClient except by:
* Do not send any request to the server;
* You can add the expected Response object;
* You can collect information from the CURL after submit the request.### Setting the expected response object
```php
object = new MockClient($expectedResponse);
$response = $mock->sendRequest(new Request("http://example.com"));assertEquals($expectedResponse, $response);
```### Debuging the CURL options
```php
object = new MockClient($expectedResponse);
$response = $mock->sendRequest(new Request("http://example.com"));$expectedCurlOptions = [
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_TIMEOUT => 30,
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERAGENT => "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)",
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_SSL_VERIFYPEER => 1,
CURLOPT_HTTPHEADER => [
'Host: localhost:8080'
],
];assertEquals($expectedCurlOptions, $mock->getCurlConfiguration());
```### Other methods in the MockClient
The methods below are available *after* the execution of the method `sendRequest()`:
* getCurlConfiguration()
* getRequestedObject()
* getExpectedResponse()## Install
```bash
composer install "byjg/webrequest"
```## Running Tests
### Starting the server
We provide a docker-compose to enable start the test server easily.
```bash
docker-compose up -d
```### Running the integration tests
```bash
vendor/bin/phpunit
```### Stopping the server
```bash
docker-compose down
```## Dependencies
```mermaid
flowchart TD
byjg/webrequest --> psr/http-message
byjg/webrequest --> psr/http-client
byjg/webrequest --> ext-json
byjg/webrequest --> byjg/uri
```----
[Open source ByJG](http://opensource.byjg.com)