1. Go to this page and download the library: Download gmponos/http-message-util library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gmponos / http-message-util example snippets
class SendNotification
{
private $client;
private $requestFactory;
public function __construct(ClientInterface $client, RequestFactoryInterface $requestFactory)
{
// PSR-17 factory
$this->requestFactory = $requestFactory;
// PSR-18 HTTP Client
$this->client = $client;
}
public function send(string $email, string $message): \Psr\Http\Message\ResponseInterface
{
$request = $this->requestFactory->create('GET', 'http://www.testurl.com');
$body = $request->getBody();
if ($body->isSeekable() === false || $body->isWritable() === false) {
throw new \InvalidArgumentException('Can not modify a request with non writable body.');
}
$content = json_encode(['email' => $email, 'message' => $message]);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new \InvalidArgumentException('Json encoding failed: ' . json_last_error());
}
$body->write($content);
$body->rewind();
$request = $request->withHeader('Content-Type', 'application/json');
return $this->client->sendRequest($request);
}
}
use HttpMessageUtil\RequestUtil;
class SendNotification
{
private $client;
private $requestFactory;
public function __construct(ClientInterface $client, RequestFactoryInterface $requestFactory)
{
// PSR-17 factory
$this->requestFactory = $requestFactory;
// PSR-18 HTTP Client
$this->client = $client;
}
public function send(string $email, string $message): \Psr\Http\Message\ResponseInterface
{
$request = $this->requestFactory->create('GET', 'http://www.testurl.com');
$request = RequestUtil::withJsonBody($request, [
'email' => $email,
'message' => $message
]);
return $this->client->sendRequest($request);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.