Getting Started
Learn how to use the Instructor HTTP client API in your PHP project.
Installation
The Instructor HTTP client API is part of the Instructor library (https://instructorphp.com) and is bundled with it.
You can install it separately via Composer:
Dependencies
The Instructor HTTP client API requires at least one of the supported HTTP client libraries. Depending on which client you want to use, you’ll need to install the corresponding package:
For Guzzle:
For Symfony HTTP Client:
For Laravel HTTP Client: The Laravel HTTP Client is included with the Laravel framework. If you’re using Laravel, you don’t need to install it separately.
PHP Requirements
The library requires:
- PHP 8.1 or higher
- JSON extension
- cURL extension (recommended)
Basic Usage
Using the Instructor HTTP client API involves a few key steps:
- Create an
HttpClient
instance - Create an
HttpClientRequest
object - Use the client to handle the request
- Process the response0
Here’s a simple example:
Error Handling
HTTP requests can fail for various reasons. You should always wrap request handling in a try-catch block:
Configuration
The Instructor HTTP client API can be configured via configuration files or at runtime.
Configuration Files
Create the configuration files in your project:
config/http.php:
config/debug.php:
Runtime Configuration
You can also configure the client at runtime:
Simple Request Example
Let’s put everything together with a practical example of making a POST request to create a new resource:
Example: Fetching Data
Here’s an example of making a GET request to fetch data:
These examples demonstrate the basic usage of the Instructor HTTP client API for common HTTP operations. In the following chapters, we’ll explore more advanced features and customization options.