Use a proxy with PHP and CURL
Leave a reply
Here is a simple script to use a proxy in PHP with CURL.
This is an example if you are using IP authentication with the proxies:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$proxy_ip = '123.123.123.123'; //proxy IP here $proxy_port = 3128; //proxy port that you use with the proxies $url = 'http://google.com'; //URL to get $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); // no headers in the output curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // output to variable curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port); curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP'); curl_setopt($ch, CURLOPT_PROXY, $proxy_ip); $data = curl_exec($ch); curl_close($ch); echo $data; |
If you are using the proxies with username/password authentication use the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$loginpassw = 'login:password'; //your proxy login and password here $proxy_ip = '123.123.123.123'; //proxy IP here $proxy_port = 8080; //proxy port that you use with the proxies $url = 'http://google.com'; //URL to get $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); // no headers in the output curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // output to variable curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port); curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP'); curl_setopt($ch, CURLOPT_PROXY, $proxy_ip); curl_setopt($ch, CURLOPT_PROXYUSERPWD, $loginpassw); $data = curl_exec($ch); curl_close($ch); echo $data; |