Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a SOCKS 5 proxy with cURL?

Tags:

Normal proxies (ex: 72.41.132.22:3128) work well with cURL, however when I use SOCKS 5 proxies with username/pass, It just gives me "[1" on the page.

Is there a way to use SOCKY 5 proxies with cURL ?

$proxy = "cagsan:[email protected]:34792";  $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_PROXY, $proxy); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HEADER, 1); $curl_scraped_page = curl_exec($ch); curl_close($ch); 
like image 266
user198989 Avatar asked Nov 18 '12 21:11

user198989


People also ask

Does Curl go through proxy?

To use a proxy with Curl, you must pass the required proxy address using the -x (or --proxy) command-line option and proxy credentials using the -U (or --proxy-user) command-line switch. Proxy credentials may also be passed in the proxy string and will be URL decoded by Curl.

How does curl proxy work?

Proxies - Everything curl. A proxy is a machine or software that does something on behalf of you, the client. You can also see it as a middle man that sits between you and the server you want to work with, a middle man that you connect to instead of the actual remote server.


1 Answers

You need to tell cURL the proxy is a SOCKS5 proxy, otherwise cURL assumes it's an HTTP proxy:

curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); 

From the docs:

CURLOPT_PROXYTYPE

Either CURLPROXY_HTTP (default) or CURLPROXY_SOCKS5.

like image 157
Kelvin Avatar answered Sep 20 '22 08:09

Kelvin