Page 1 of 1

cURL » Setting Proxies w/ curl

Posted: Wed Jul 07, 2010 12:30 pm
by shanehunter
Hey Guys, here's the code:

Code: Select all

$username = 'username';
$password = 'password';
$url = 'http://identi.ca/api/statuses/update.xml';
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 2);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status= $s0 $awesome");
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
I've read in some places, that using something like this:

Code: Select all

curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
curl_setopt($ch, CURLOPT_PROXY, '128.114.63.15:3128');
I can set a proxy, for use with my cURL request?

I'm not really sure where, or how to implement this into my code. Any help would be greatly appreciated.

Second question, how do I use an array for the proxy value (ie: use more than one proxy, which is randomly chosen when the script is executed)?

Thanks again guys!

Re: cURL » Setting Proxies w/ curl

Posted: Wed Jul 07, 2010 12:49 pm
by Bind

Code: Select all

<?php
$proxies = array(
                    '128.114.63.15:3128',
                    '128.114.63.16:3128',
                    '128.114.63.17:3128',
                    '128.114.63.18:3128',
                    '128.114.63.19:3128',
                    '128.114.63.20:3128',
                    '128.114.63.21:3128',
                    '128.114.63.22:3128',
                    '128.114.63.23:3128',
                    '128.114.63.24:3128',
                    '128.114.63.25:3128',
                    '128.114.63.26:3128',
                    '128.114.63.27:3128',
                );
$random_proxy = array_rand(array_flip($proxies),1);
$username = 'username';
$password = 'password';
$url = 'http://identi.ca/api/statuses/update.xml';
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 2);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status= $s0 $awesome");
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl_handle, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($curl_handle, CURLOPT_PROXY, $random_proxy);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
?>
edited to correct bad cURL handler

Re: cURL » Setting Proxies w/ curl

Posted: Wed Jul 07, 2010 1:53 pm
by shanehunter
That's returning the following error:

Empty
Warning: curl_setopt(): supplied argument is not a valid cURL handle resource in /home/shane/public_html/identi/iei.php on line 161

Warning: curl_setopt(): supplied argument is not a valid cURL handle resource in /home/shane/public_html/identi/iei.php on line 162
success!


161. curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
162. curl_setopt($ch, CURLOPT_PROXY, $random_proxy);

Re: cURL » Setting Proxies w/ curl

Posted: Wed Jul 07, 2010 2:17 pm
by Bind
was using your example which contained an incorrect cURL Handle Name that I did not alter to fit your script ... try this:

Code: Select all

<?php
$proxies = array(
                    '128.114.63.15:3128',
                    '128.114.63.16:3128',
                    '128.114.63.17:3128',
                    '128.114.63.18:3128',
                    '128.114.63.19:3128',
                    '128.114.63.20:3128',
                    '128.114.63.21:3128',
                    '128.114.63.22:3128',
                    '128.114.63.23:3128',
                    '128.114.63.24:3128',
                    '128.114.63.25:3128',
                    '128.114.63.26:3128',
                    '128.114.63.27:3128',
                );
$random_proxy = array_rand(array_flip($proxies),1);
$username = 'username';
$password = 'password';
$url = 'http://identi.ca/api/statuses/update.xml';
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 2);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status= $s0 $awesome");
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl_handle, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($curl_handle, CURLOPT_PROXY, $random_proxy);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
?>

Re: cURL » Setting Proxies w/ curl

Posted: Wed Jul 07, 2010 5:06 pm
by shanehunter
awesome! thanks man.