Response Interface
All responses implement theHttpResponse interface, which provides a uniform way to access response data:
Getting the Response
When you send a request using theHttpClient::handle() method, it returns an implementation of HttpResponse:
PsrHttpResponse: Used by the GuzzleDriverSymfonyHttpResponse: Used by the SymfonyDriverLaravelHttpResponse: Used by the LaravelDriverMockHttpResponse: Used by the MockHttpDriver for testing
Status Codes
The status code indicates the result of the HTTP request. You can access it using thestatusCode() method:
Status Code Categories
Status codes are grouped into categories:- 1xx (Informational): The request was received and understood
- 2xx (Success): The request was successfully received, understood, and accepted
- 3xx (Redirection): Further action needs to be taken to complete the request
- 4xx (Client Error): The request contains bad syntax or cannot be fulfilled
- 5xx (Server Error): The server failed to fulfill a valid request
Checking Response Status
You can check if a response was successful:Common Status Codes
Here are some common HTTP status codes you might encounter:- 200 OK: The request was successful
- 201 Created: A new resource was successfully created
- 204 No Content: The request was successful, but there’s no response body
- 400 Bad Request: The request was malformed or invalid
- 401 Unauthorized: Authentication is required
- 403 Forbidden: The client doesn’t have permission to access the resource
- 404 Not Found: The requested resource doesn’t exist
- 405 Method Not Allowed: The HTTP method is not supported for this resource
- 422 Unprocessable Entity: The request was well-formed but contains semantic errors
- 429 Too Many Requests: Rate limit exceeded
- 500 Internal Server Error: A generic server error occurred
- 502 Bad Gateway: The server received an invalid response from an upstream server
- 503 Service Unavailable: The server is temporarily unavailable
- 504 Gateway Timeout: The upstream server didn’t respond in time
- 511 Network Authentication Required: The client needs to authenticate to gain network access
Headers
Response headers provide metadata about the response. You can access the headers using theheaders() method:
Content-Type), while others might use lowercase (e.g., content-type).
Common Response Headers
Here are some common response headers you might encounter:- Content-Type: The MIME type of the response body
- Content-Length: The size of the response body in bytes
- Cache-Control: Directives for caching mechanisms
- Set-Cookie: Cookies to be stored by the client
- Location: Used for redirects
- X-RateLimit-Limit: The rate limit for the endpoint
- X-RateLimit-Remaining: The number of requests remaining in the current rate limit window
- X-RateLimit-Reset: When the rate limit will reset
Body Content
For non-streaming responses, you can get the entire response body as a string using thebody() method:
Processing JSON Responses
Many APIs return JSON responses. You can decode them using PHP’sjson_decode() function:
Processing XML Responses
For XML responses, you can use PHP’s built-in XML functions:Processing Binary Responses
For binary responses (like file downloads), you can save the response body to a file:Error Handling
When making HTTP requests, various errors can occur. The Instructor HTTP client API provides a consistent way to handle these errors through exceptions.RequestException
The main exception type isRequestException, which is thrown when a request fails:
RequestException often wraps another exception from the underlying HTTP client, which you can access with $e->getPrevious().
Error Response Handling
By default, HTTP error responses (4xx, 5xx status codes) do not throw exceptions. You can control this behavior using thefailOnError configuration option:
failOnError is set to true, the client will throw a RequestException for error responses. When it’s false, you need to check the status code yourself: