Facebooktwittergoogle_plusredditpinterestlinkedinmail

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");

curl_setopt options
CURLOPT_URL The URL to fetch. This can also be set when initializing a session with curl_init().
CURLOPT_PROXY The HTTP proxy to tunnel requests through.
CURLOPT_FOLLOWLOCATION TRUE to follow any “Location: ” header that the server sends as part of the HTTP header
CURLOPT_RETURNTRANSFER TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
CURLOPT_HEADER TRUE to include the header in the output.
CURLOPT_PROXYUSERPWD A username and password formatted as “[username]:[password]” to use for the connection to the proxy

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
How to use CURL with Proxyhttp://techieroop.com/wp-content/uploads/2015/07/how-to-use-php-curl-on-proxy-server.pnghttp://techieroop.com/wp-content/uploads/2015/07/how-to-use-php-curl-on-proxy-server-150x150.png Roopendra PHP,,,
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 =...
This post <span id="GingerWidget-correction-0" class="correction alternate">will be demonstrating</span> how to use curl with proxy in <span id="GingerWidget-correction-1" class="correction alternate">PHP</span> 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 <span id="GingerWidget-correction-2" class="correction alternate">PHP</span> curl.<strong>1) PHP curl with proxy</strong> <pre>$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;</pre> Please add <strong>CURLOPT_PROXYUSERPWD</strong> If you configured proxy with credentials. <pre>curl_setopt($ch, CURLOPT_PROXYUSERPWD, "username:password");</pre><strong>2) Proxy In linux Command Line:</strong> <pre>Syntax:  curl <URL> --proxy <PROXY_HOST>:<PROXY_PORT> curl http://localhost/project/file.jpg --proxy http://127.0.0.1:3128</pre> In <span id="GingerWidget-correction-0" class="correction alternate">Linux</span> we have also <span id="GingerWidget-correction-1" class="correction alternate">the capability</span> to configure different proxy through environment variables like http_proxy<span id="GingerWidget-correction-2" class="correction alternate">, </span>ftp_proxy and https_proxy. <pre>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</pre>
(Visited 2,082 times, 34 visits today)
Facebooktwittergoogle_pluslinkedinrss