How to use CURL via a proxy?
Here is a working version with your bugs removed.
$url = 'http://dynupdate.no-ip.com/ip.php';$proxy = '127.0.0.1:8888';//$proxyauth = 'user:password';$ch = curl_init();curl_setopt($ch, CURLOPT_URL,$url);curl_setopt($ch, CURLOPT_PROXY, $proxy);//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_HEADER, 1);$curl_scraped_page = curl_exec($ch);curl_close($ch);echo $curl_scraped_page;
I have added CURLOPT_PROXYUSERPWD
in case any of your proxies require a user name and password.I set CURLOPT_RETURNTRANSFER
to 1, so that the data will be returned to $curl_scraped_page
variable.
I removed a second extra curl_exec($ch);
which would stop the variable being returned.I consolidated your proxy IP and port into one setting.
I also removed CURLOPT_HTTPPROXYTUNNEL
and CURLOPT_CUSTOMREQUEST
as it was the default.
If you don't want the headers returned, comment out CURLOPT_HEADER
.
To disable the proxy simply set it to null.
curl_setopt($ch, CURLOPT_PROXY, null);
Any questions feel free to ask, I work with cURL
every day.
I have explained use of various CURL options required for CURL PROXY.
$url = 'http://dynupdate.no-ip.com/ip.php';$proxy = '127.0.0.1:8888';$proxyauth = 'user:password';$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url); // URL for CURL callcurl_setopt($ch, CURLOPT_PROXY, $proxy); // PROXY details with portcurl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth); // Use if proxy have username and passwordcurl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); // If expected to call with specific PROXY typecurl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // If url has redirects then go to the final redirected URL.curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); // Do not outputting it out directly on screen.curl_setopt($ch, CURLOPT_HEADER, 1); // If you want Header information of response else make 0$curl_scraped_page = curl_exec($ch);curl_close($ch);echo $curl_scraped_page;
root@APPLICATIOSERVER:/var/www/html# php connectiontest.php61e23468-949e-4103-8e08-9db09249e8s1 OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to 10.172.123.1:80 root@APPLICATIOSERVER:/var/www/html#
Post declaring the proxy settings in the php script file issue has been fixed.
$proxy = '10.172.123.1:80';curl_setopt($cSession, CURLOPT_PROXY, $proxy); // PROXY details with port