将PHP代码转换为curl命令,其中只允许TLSv1

问题描述:

如何将此代码转换为curl命令?我想通过执行单个curl命令在linux机器上使用此代码。将PHP代码转换为curl命令,其中只允许TLSv1

$headers = array(
     "Content-type: text/xml", 
     "Content-length: " . strlen($xml) 
); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $this->url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 10000); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

$data = curl_exec($ch); 

我厌倦了这一个,但不成功:

curl -X POST -H "Content-type: text/xml" -o output.txt -d "param1=param1&username=username&password=password" https://site.url.com -d @data.xml 

也许问题是在HTTPS,因为只有使用TLSv1被允许在网站上。

+0

由curl或响应报告的任何错误? – NDM 2014-10-28 15:33:01

+0

@NDM这是每次尝试的响应: 401 Unauthorized>,来自php和浏览器一切正常 – ibedelovski 2014-10-28 15:35:57

+0

401意味着TLS层很好,但这意味着你没有提供正确的HTTP认证。 – 2014-10-28 15:50:18

如果要强制卷曲使用的TLSv1,您可以使用--tlsv1选项,从the docs

-1, --tlsv1 

(SSL) Forces curl to use TLS version 1.x when negotiating with a remote TLS server. You can use options --tlsv1.0, --tlsv1.1, and --tlsv1.2 to control the TLS version more precisely (if the SSL backend in use supports such a level of control). 

-2, --sslv2 

(SSL) Forces curl to use SSL version 2 when negotiating with a remote SSL server. Sometimes curl is built without SSLv2 support. SSLv2 is widely considered insecure. 

-3, --sslv3 

(SSL) Forces curl to use SSL version 3 when negotiating with a remote SSL server. Sometimes curl is built without SSLv3 support. 
+0

好信息 – fortune 2014-10-28 15:55:37

问题是不相关的TLS/SSL。客户端将自动协商TLS。 看来你需要做一些POST一些XML数据,并指定你的凭据作为GET参数。 它可以把你的GET参数请求URL

林不知道的语法来完成,但试试这个:

curl -X POST -H "Content-type: text/xml" -o output.txt https://site.url.com?param1=param1&username=username&password=password -d @data.xml 

另外,(小offtopic对于上述消息,但我cannt评论吧)请勿强制使用SSL2,SSL3或TLS1.0,因为它们存在漏洞。大多数服务器会自动协商最佳版本的TLS。

+0

是的你对这些漏洞是正确的,但我已经尝试了你的样本,并且再次尝试了401次。 – ibedelovski 2014-10-28 16:29:19

在PHP中你可以使用:

curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_1);

文档讲更多的TLS版本:

http://www.php.net/manual/en/function.curl-setopt.php

  • CURL_SSLVERSION_TLSv1_0
  • CURL_SSLVERSION_TLSv1_1
  • CURL_SSLVERSION_TLSv1_2

TLS版本仅适用于CURL版本7.34或更新的版本。

+0

这应该被编辑为“TLS常量只能在CURL版本7.34或更新的版本中工作。“ 同样的PHP手册页提到表明常量仅仅代表整数(所以你仍然可以利用这一点,只是在整数Sub您需要):CURL_SSLVERSION_DEFAULT(0),CURL_SSLVERSION_TLSv1(1),CURL_SSLVERSION_SSLv2(2),CURL_SSLVERSION_SSLv3( 3),CURL_SSLVERSION_TLSv1_0(4),CURL_SSLVERSION_TLSv1_1(5)或CURL_SSLVERSION_TLSv1_2(6) – 2016-05-17 23:09:00