To set a proxy server for cURL requests in PHP, you can use the CURLOPT_PROXY
option in conjunction with other relevant options. Here's an example:
// Initialize cURL
$ch = curl_init();
// Set the target URL
$targetUrl = 'http://example.com';
curl_setopt($ch, CURLOPT_URL, $targetUrl);
// Set the proxy server details
$proxyServer = 'proxy.example.com';
$proxyPort = 8080;
// Uncomment the line below if authentication is required for the proxy server
// $proxyAuth = 'username:password';
// Set the proxy server
curl_setopt($ch, CURLOPT_PROXY, $proxyServer);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxyPort);
// Uncomment the line below if authentication is required for the proxy server
// curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyAuth);
// Set other cURL options as needed
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the cURL request
$response = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
// Close cURL
curl_close($ch);
// Process the response as needed
echo $response;
Make sure to replace 'http://example.com'
, 'proxy.example.com'
, 8080
, and 'username:password'
with the appropriate values for your setup. Additionally, you might need to adjust other cURL options according to your requirements.