How to send parameters using guzzle client in symfony?

by wilmer.lemke , in category: PHP Frameworks , 2 months ago

How to send parameters using guzzle client in symfony?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dedrick , 2 months ago

@wilmer.lemke 

To send parameters using Guzzle client in Symfony, you can use the query method on the Guzzle client object. Here is an example of how to send parameters using Guzzle client in Symfony:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
use SymfonyComponentHttpClientHttpClient;
use SymfonyContractsHttpClientExceptionTransportExceptionInterface;

try {
    $client = HttpClient::create();
    $response = $client->request('GET', 'https://api.example.com/resource', [
        'query' => [
            'param1' => 'value1',
            'param2' => 'value2'
        ]
    ]);

    $content = $response->getContent();
    // Process the response content here

} catch (TransportExceptionInterface $e) {
    // Handle any exceptions that may occur
}


In this example, we are using the query option to send parameters with a GET request to the specified URL. You can add as many parameters as needed by adding key-value pairs to the query array.


Remember to handle any exceptions that may occur while making the request using Guzzle client in Symfony.