use Http\Client\HttpClient; use Http\Message\RequestFactory; use Http\Message\ResponseFactory; $httpClient = new HttpClient(); $requestFactory = new RequestFactory(); $responseFactory = new ResponseFactory(); $request = $requestFactory->createRequest('GET', 'https://jsonplaceholder.typicode.com/posts'); $response = $httpClient->sendRequest($request); $content = $response->getBody()->getContents();
use Http\Client\HttpClient; use Http\Message\RequestFactory; use Http\Message\ResponseFactory; use Psr\Http\Message\RequestInterface; $httpClient = new HttpClient(); $requestFactory = new RequestFactory(); $responseFactory = new ResponseFactory(); $request = $requestFactory->createRequest('POST', 'https://jsonplaceholder.typicode.com/posts', [ 'Content-Type' => 'application/json' ]); $request->getBody()->write(json_encode([ 'title' => 'foo', 'body' => 'bar', 'userId' => 1 ])); $response = $httpClient->sendRequest($request); $content = $response->getBody()->getContents();In this example, the getResponse() method is called on the response object obtained from sending a POST request using the Http client library. The POST request includes a JSON payload, and the response content is assigned to the $content variable.