2014-10-28 101 views
3

如何將此代碼轉換爲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,來自php和瀏覽器一切正常 – ibedelovski 2014-10-28 15:35:57

+0

401意味着TLS層很好,但這意味着你沒有提供正確的HTTP認證。 – 2014-10-28 15:50:18

回答

1

如果要強制捲曲使用的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

1

問題是不相關的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

4

在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