php 如何使用带有 cURL 的 SOCKS 5 代理?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13444738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:27:35  来源:igfitidea点击:

How to use a SOCKS 5 proxy with cURL?

phpcurlproxysocks

提问by user198989

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.

普通代理(例如72.41.132.22:3128:)与 cURL 配合良好,但是当我使用带有用户名/密码的 SOCKS 5 代理时,它只会在页面上给我“[1”。

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

有没有办法在 cURL 中使用 SOCKY 5 代理?

$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);

回答by Kelvin

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

您需要告诉 cURL 代理是 SOCKS5 代理,否则 cURL 假定它是 HTTP 代理:

curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);

From the docs:

文档

CURLOPT_PROXYTYPE

Either CURLPROXY_HTTP (default) or CURLPROXY_SOCKS5.

CURLOPT_PROXYTYPE

CURLPROXY_HTTP(默认)或 CURLPROXY_SOCKS5。

回答by GiDo

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

从 cURL 7.21.7 开始,您可以使用CURLOPT_PROXY和指定 SOCKS 协议:

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

More informations in the libcurl documentation.

libcurl 文档中的更多信息。

回答by iautomation

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

对于那些希望通过主机名(本地主机?)进行连接并且它不使用 的人CURLPROXY_SOCKS5,您可以尝试CURLPROXY_SOCKS5_HOSTNAME.

curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);

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

在一些较早的 PHP 版本中,您必须执行以下操作:

curl_setopt($ch, CURLOPT_PROXYTYPE, 7);