22

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:jw22wdw@108.61.25.223: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);
user198989
  • 4,574
  • 19
  • 66
  • 95

3 Answers3

48

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.

Kelvin
  • 5,227
  • 1
  • 23
  • 36
  • As @Gido mentioned you can skip this param and use `CURLOPT_PROXY` with prefix `socks5://` and you're good to go. [docs](https://curl.haxx.se/libcurl/c/CURLOPT_PROXY.html) – Spock Feb 01 '17 at 06:13
32

since cURL 7.21.7, you can use CURLOPT_PROXY and specify the SOCKS protocol:

curl_setopt($ch, CURLOPT_PROXY, 'socks5://bob:marley@localhost:12345');

More informations in the libcurl documentation.

Saa
  • 1,540
  • 10
  • 22
GiDo
  • 1,280
  • 12
  • 23
4

For those looking to connect via a hostname(localhost?), and it's not working with CURLPROXY_SOCKS5, you can try CURLPROXY_SOCKS5_HOSTNAME.

curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);

In some earlier PHP versions, you will have to do:

curl_setopt($ch, CURLOPT_PROXYTYPE, 7);
iautomation
  • 996
  • 10
  • 18
  • https://curl.haxx.se/libcurl/c/CURLOPT_PROXY.html - CURLPROXY_SOCKS5_HOSTNAME indicates that hostname resolution should be performed by socks server. – Slotos Mar 17 '16 at 12:09
  • my PHP version `PHP 7.0.22-0ubuntu0.16.04.1 (cli) ( NTS )` and `curl_setopt($ch, CURLOPT_PROXYTYPE, 7);` works to me, thanks – Frank He Sep 06 '17 at 11:23