How to set a proxy server for Guzzle requests in PHP?

To set a proxy server for Guzzle requests in PHP, you can use the request_options parameter when making a Guzzle HTTP request. Here's a step-by-step guide:

  1. Install Guzzle using Composer if you haven't already:
composer require guzzlehttp/guzzle
  1. Include the Guzzle namespace in your PHP file:
use GuzzleHttp\Client;
  1. Create a new instance of the Guzzle client:
$client = new Client();
  1. Define the proxy server details:
$proxy = [ 'http' => 'tcp://proxy.example.com:3128', // Replace with your proxy server URL and port 'https' => 'tcp://proxy.example.com:3128' // Replace with your proxy server URL and port ];
  1. Pass the proxy server details in the request_options parameter when making the Guzzle request:
$response = $client->request('GET', 'https://api.example.com', [ 'proxy' => $proxy ]);

Now, Guzzle will send the request through the specified proxy server. Make sure to replace 'https://api.example.com' with the actual URL you want to make the request to.

Note: Guzzle supports multiple proxy types (HTTP, SOCKS4, SOCKS4a, and SOCKS5) based on the proxy URL scheme. Adjust the proxy URL accordingly based on your proxy server type.