Laravel Guzzle HTTP Client Request-Response Stats

Laravel 8 introduced an HTTP Client which is a simple wrapper around the famous PHP Guzzle library. It works great out of the box, but here are some additional statistics that you can get out of every request and response.

Sample Request

use Http;

$response = Http::get("https://jsonplaceholder.typicode.com/todos");

Here we are making a sample API call to the JsonPlaceHolder Fake Rest API. The result of the API call is stored in the $response variable. Let's print the $response variable and see what it provides.

Response Variable Printed

Out of all these details, we are interested in the transfer stats object. Let's see what is available inside $response->transferStats->getHandlerStats() or $response->handlerStats()

Here is how it looks like

Handler Stats Printed

Here is the description of all available information inside the handler stats array.

  • url - Last effective URL. This might be useful to know in case the initial API performs some redirections

  • content_type - Content-Type: of the requested document. NULL indicates the server did not send a valid Content-Type: header

  • http_code - The last response code

  • header_size - Total size of all headers received

  • request_size - Total size of issued requests

  • filetime - Remote time of the retrieved document, with the CURLOPT_FILETIME enabled; if -1 is returned the time of the document is unknown

  • ssl_verify_result -Result of SSL certification verification requested by setting

  • redirect_count - Number of redirects, with the CURLOPT_FOLLOWLOCATION option enabled

  • total_time - Total transaction time in seconds for the last transfer

  • namelookup_time - Time in seconds until name resolving was complete

  • connect_time - Time in seconds it took to establish the connection

  • pretransfer_time - Time in seconds from start until just before file transfer begins

  • size_upload - Total number of bytes uploaded

  • size_download - Total number of bytes downloaded

  • speed_download - Average download speed

  • speed_upload - Average upload speed

  • download_content_length - Content length of download, read from Content-Length: field

  • upload_content_length - Specified size of upload

  • starttransfer_time - Time in seconds until the first byte is about to be transferred

  • redirect_time - Time in seconds of all redirection steps before the final transaction was started, with the CURLOPT_FOLLOWLOCATION option enabled

  • redirect_url - With the CURLOPTFOLLOWLOCATION option disabled: redirect URL found in the last transaction, which should be requested manually next. With the CURLOPTFOLLOWLOCATION option enabled: this is empty. The redirect URL in this case is available in CURLINFOEFFECTIVEURL

  • primary_ip - IP address of the most recent connection

  • certinfo - TLS certificate chain

  • primary_port - Destination port of the most recent connection

  • local_ip - Local (source) IP address of the most recent connection

  • local_port - Local (source) port of the most recent connection

  • http_version - The version used in the last HTTP connection. The return value will be one of the defined CURLHTTPVERSION_* constants or 0 if the version can't be determined

  • protocol - The protocol used in the last HTTP connection. The returned value will be exactly one of the CURLPROTO_* values

  • scheme - The URL scheme used for the most recent connection

  • appconnect_time_us - Time, in microseconds, it took from the start until the SSL/SSH connect/handshake to the remote host was complete

  • connect_time_us - Total time taken, in microseconds, from the start until the connection to the remote host (or proxy) was completed

  • namelookup_time_us - Time in microseconds from the start until the name resolving was completed

  • pretransfer_time_us - Time taken from the start until the file transfer is just about to begin, in microseconds

  • redirect_time_us - Total time, in microseconds, it took for all redirection steps including name lookup, connect, pretransfer, and transfer before the final transaction was started

  • starttransfer_time_us - Time, in microseconds, it took from the start until the first byte is received

  • total_time_us - Total time in microseconds for the previous transfer, including name resolving, TCP connect, etc.

  • appconnect_time - Time in seconds it took from the start until the SSL/SSH connect/handshake to the remote host was completed

    You can find more information on this in this PHP Manual