How to use CURL with Proxy
This post will be demonstrating how to use curl with proxy in PHP and Linux command line. What is additional parameter we need to pass while using curl via proxy. So I am starting with proxy configuration in PHP curl.
1) PHP curl with proxy
$url = 'http://localhost/path/to/file.php'; $proxy = '127.0.0.2:3128'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_PROXY, $proxy); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_HEADER, TRUE); $result = curl_exec($ch); curl_close($ch); echo $result;
Please add CURLOPT_PROXYUSERPWD If you configured proxy with credentials.
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "username:password");
2) Proxy In linux Command Line:
Syntax: curl <URL> --proxy <PROXY_HOST>:<PROXY_PORT> curl http://localhost/project/file.jpg --proxy http://127.0.0.1:3128
In Linux we have also the capability to configure different proxy through environment variables like http_proxy, ftp_proxy and https_proxy.
export http_proxy=http://127.0.0.2:3128 #for password protected proxy export http_proxy=http://user:password@127.0.0.2:3128 #to support https url export https_proxy=http://127.0.0.2:3128
(Visited 2,953 times, 15 visits today)