How to use proxy with curl php
Using proxies with PHP cURL is a relatively straightforward process. Here's a step-by-step tutorial:
-
Start by initializing your cURL handle using the curl_init() function:
$ch = curl_init();
-
Next, set the proxy options using the CURLOPT_PROXY and CURLOPT_PROXYPORT options. CURLOPT_PROXY sets the IP address or hostname of the proxy server, while CURLOPT_PROXYPORT sets the port number:
curl_setopt($ch, CURLOPT_PROXY, 'proxy.example.com'); curl_setopt($ch, CURLOPT_PROXYPORT, 8080);
-
If the proxy server requires authentication, you can set the username and password using the CURLOPT_PROXYUSERPWD option:
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'username:password');
-
Set the URL of the page you want to fetch using the CURLOPT_URL option:
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/');
-
Finally, execute the cURL request using curl_exec():
And that's it! Here's the full code for reference:$result = curl_exec($ch);
$ch = curl_init(); curl_setopt($ch, CURLOPT_PROXY, 'proxy.example.com'); curl_setopt($ch, CURLOPT_PROXYPORT, 8080); curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'username:password'); curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/'); $result = curl_exec($ch); curl_close($ch);