Reputation: 1211
I want to get information about the request I have sent, such as url, body sent etc. I am using the Async feature which uses promises (example below)
$client = new \GuzzleHttp\Client();
return new \GuzzleHttp\Psr7\Request\Request('POST', $this->getUrl(), $this->getHeaders(), $this->getBody());
Is there a way where I can get request information from the promise or from the response?
Reason I am asking this is because I need to store some information about the request in the database later on, which can not be done before I sent the request.
$promise->getRequest()
$pomise->Request
$promise->request
$promise->getHandlers()
Thank you
Upvotes: 2
Views: 2886
Reputation: 48711
When you initialize a new Request
then you have to send it. It's not sent by default. A request is sent when a Client
calls send
method on it. When request is completed you have access to all information about response:
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Client;
$client = new Client();
$request = new Request('GET', 'https://www.google.com');
$response = $client->send($request);
$response->getHeaders(); // array of all retreived headers
$response->getStatusCode(); // http status code
$response->getReasonPhrase(); // http status phrase
and you initialized a wrong Request
object, Guzzle is not shipped with \GuzzleHttp\Psr7\Request\Request
but \GuzzleHttp\Psr7\Request
.
Now with a correct way of sending a request, getting request information is as simple as below:
print_r($request->getHeaders()); // array of all sent headers
echo $request->getUri(); // requested URI
Upvotes: 2