Creating Requests
All HTTP requests are created using theHttpRequest class, which encapsulates the various components of an HTTP request.
Basic Request Creation
The constructor forHttpRequest takes several parameters:
url: The URL to send the request to (string)method: The HTTP method to use (string)headers: An associative array of HTTP headers (array)body: The request body, which can be a string or an array (mixed)options: Additional options for the request (array)
Request Methods
Once you’ve created a request, you can access its properties using the following methods:Modifying Requests
You can also modify a request after it’s been created:with* methods return a new request instance rather than modifying the original one.
HTTP Methods
The HTTP method is specified as a string in theHttpClientRequest constructor. The library supports all standard HTTP methods:
GET Requests
GET requests are used to retrieve data from a server:POST Requests
POST requests are used to create new resources or submit data:PUT Requests
PUT requests are used to update existing resources:PATCH Requests
PATCH requests are used to partially update resources:DELETE Requests
DELETE requests are used to remove resources:Other Methods
The library also supports other HTTP methods like HEAD, OPTIONS, etc. Just specify the method name as a string:Setting Headers
HTTP headers are specified as an associative array where keys are header names and values are header values:Common Headers
Some commonly used HTTP headers include:- Content-Type: Specifies the format of the request body
- Accept: Indicates what response format the client can understand
- Authorization: Provides authentication credentials
- User-Agent: Identifies the client application
- Cache-Control: Directives for caching mechanisms
- Accept-Language: Indicates the preferred language
Request Body
The request body can be provided in two ways:Array Body (JSON)
If you provide an array as the request body, it will automatically be converted to a JSON string:Content-Type header to application/json.
String Body
You can also provide the body as a raw string:Working with Request Body
The body is managed by theHttpRequestBody class, which provides methods to access the body in different formats:
Request Options
Theoptions parameter allows you to specify additional options for the request:
Available Options
Currently, the main supported option is:stream: When set totrue, enables streaming response handling