sal / php-seven
PHP ISO/OSI layer 7 adapters library.
v4.0.0
2025-08-12 07:31 UTC
Requires
- php: ^8.2
- guzzlehttp/guzzle: ^7.9
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.78
- phpstan/phpstan: ^1.10
- phpstan/phpstan-symfony: ^1.3
- phpunit/phpunit: ^10.5
- rector/rector: ^1.2
README
php-seven
PHP ISO/OSI
layer 7 protocols adapters library.
Icon by smalllikeart - Flaticon
Why this library?
This library speeds up developing of clients via common layer 7 communication protocols, providing ready-to-use
and dependency-injection-friendly
adapters.
Protocols
Below the exaustive list of supported protocols.
HTTP(s)
authenticated viaBasic
andBearer
SSH
authenticated viaIdentityFile
(asymmetric key pair)
Installation
composer require sal/php-seven
Usage
class MyAwesomeClient { public function __construct() { // Require the instance to dependency injection private HttpAdapterInterface $http; } ( // Set a custom timeout to HTTP requests $this->http->setTimeout(20); // Avoid TLS certificate validation $this->http->setVerify(false); // Add custom headers $this->http->addHeader(HttpHeaderFactory::accept(ContentType::JSON)); ); // Login (Bearer) public function login(string $server, string $username, string $password): void { // Set the base URI $this->http->setBaseUri("https://{$server}"); // Perform HTTP post $response = $this->http->post( '/login', [ // Add Form HTTP parameters new HttpParameter('username', $username), new HttpParameter('password', $password), ] ); if (401 === $response->getStatusCode()) { throw new RuntimeException('Invalid credentials.'); } // Retrieve the Bearer token from the response $body = json_decode($response->getBody() ?? '', true); if (!isset($body['token'])) { throw new RuntimeException('The server has not started the session.'); } $token = $body['token']; // Set the token to further uses $this->http->setAuthorization(new HttpBearerAuthentication($token)); } // Logout the adapter public function logout(): void { $this->http->setAuthorization(null); } // Use the logged in adapter public function addUser(string $email): void { $response = $this->http->post( '/add-user', [ new HttpParameter('email', $email), ] ); // Use the isSuccessful() method to check if the status code is < 400. if (!$response->isSuccessful()) { throw new RuntimeException("Response code: {$response->getStatusCode()}"); } } }
Contribution
Contributions are always welcomed (both issues opening and pull requests).
Improve the code
Fork the project, perform your improvements and open a pull request. Note: pull requests MUST include related unit tests and related documentation if needed. All pull requests MUST be linked to an issue.