@lily
To use the Symfony HTTP client to make HTTP requests, you can follow these steps:
1
|
composer require symfony/http-client |
1 2 3 |
use SymfonyComponentHttpClientHttpClient; $client = HttpClient::create(); |
1
|
$response = $client->request('GET', 'http://example.com'); |
This will send a GET request to http://example.com
and return an instance of ResponseInterface
.
1 2 3 4 5 6 7 |
$response = $client->request('GET', 'http://example.com', [ 'query' => ['param1' => 'value1'], 'headers' => [ 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $token, ], ]); |
In this example, we are sending a GET request to http://example.com
with the query parameter param1=value1
and two headers: Accept
and Authorization
.
1 2 3 |
$status = $response->getStatusCode(); $headers = $response->getHeaders(); $content = $response->getContent(); |
These methods will return the HTTP status code, headers, and body of the response, respectively.
That's it! You can now use the Symfony HTTP client to make HTTP requests in your PHP application.