使用socks5代理的cURL请求在使用PHP时失败,但它通过命令行工作 [英] cURL request using socks5 proxy fails when using PHP, but it works through the command line

查看:2594
本文介绍了使用socks5代理的cURL请求在使用PHP时失败,但它通过命令行工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cURL +代理noob在这里,有一个困难的时间。我无法通过代理从远程安全服务器检索网页。一切都显然是由一个远程开发正确设置,以便下面的命令行指令工作,并返回我们正在寻找:

  curl -k --socks5-hostname localhost:xxxx https:// hostname / 

,以下PHP不会回显请求的网页。相反,它回显错误无法解析主机名:

  $ proxy ='localhost:xxxx'; 
$ url ='https:// hostname /';
// $ proxyauth ='user:password';

$ ch = curl_init();
curl_setopt($ ch,CURLOPT_PROXY,$ proxy);
curl_setopt($ ch,CURLOPT_PROXYTYPE,CURLPROXY_SOCKS5);
// curl_setopt($ ch,CURLOPT_PROXYUSERPWD,$ proxyauth);
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_HEADER,1);
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ ch,CURLOPT_URL,$ url);
$ curl_scraped_pa​​ge = curl_exec($ ch);
$ error = curl_error($ ch);
curl_close($ ch);

if($ error)
echo $ error;
elseif($ curl_scraped_pa​​ge)
echo $ curl_scraped_pa​​ge;

如果$ url已更改为公共网页很高兴。



如果更改任何东西,连接需要一个SSH隧道。



这里有没有明显的错误?



看起来CURLPROXY_SOCKS5_HOSTNAME在PHP中未定义,但您可以明确使用它的值等于7。 p>尝试:

  curl_setopt($ ch,CURLOPT_PROXYTYPE,7); 


cURL + proxy noob here, having a hard time. I'm having trouble trying to retrieve a web page from a remote secure server via a proxy. Everything has apparently been set up correctly by a remote dev, such that the following command line instruction works and returns what we're looking for:

curl -k --socks5-hostname localhost:xxxx https://hostname/

However, the following PHP does not echo the requested webpage. Instead it echoes the error 'Couldn't resolve host name':

$proxy = 'localhost:xxxx';
$url = 'https://hostname/';
//$proxyauth = 'user:password';

$ch = curl_init();
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
$curl_scraped_page = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);

if ($error)
    echo $error;
elseif ($curl_scraped_page)
    echo $curl_scraped_page;

If the $url is changed to a public page, such as Google, the request is successful and everyone is happy.

The connection requires an SSH tunnel if that changes anything at all. The tunnel is open and functioning, as proven by the command line request succeeding.

Is there something obvious that is being missed here?

解决方案

It looks like CURLPROXY_SOCKS5_HOSTNAME is not defined in PHP, but you can explicitly use its value which equals 7.

Try this:

curl_setopt($ch, CURLOPT_PROXYTYPE, 7);

这篇关于使用socks5代理的cURL请求在使用PHP时失败,但它通过命令行工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆