2016-12-28 47 views
2

我正在使用soap在soap中爲web服務製作一個客戶端。 web服務使用自簽名證書通過https運行,對於測試,我必須信任此證書而不安裝它。PHP肥皂ssl如何信任自簽名證書

的問題是,我百達得到這個錯誤:

SOAP的錯誤:解析WSDL:無法從「https://winsystemsintl.com:54904/PSAService.svc?wsdl」中加載:未能加載外部實體「https://winsystemsintl.com:54904/PSAService.svc?wsdl」。

這裏是我的代碼:

$opts = [ 
     'ssl' => [ 
      // set some SSL/TLS specific options 
      'verify_peer' => false, 
      'verify_peer_name' => false, 
      'allow_self_signed' => true 
     ], 
     'http'=>[ 
      'user_agent' => 'PHPSoapClient' 
     ] 
    ]; 

    // Initialize Soap Client 
    $this->client = new SoapClient($this->wsdl, array('ssl_method' => SOAP_SSL_METHOD_SSLv3,'soap_version' => SOAP_1_2, 'location' => 'https://winsystemsintl.com:54904/PSAService.svc','stream_context' => stream_context_create($opts), 'exceptions' => true, 'trace' => true)); 

我能夠用wget來獲得WSDL:

wget的--secure協議= SSLv3的https://winsystemsintl.com:54904/PSAService.svc?wsdl --no檢查證書

希望有人能幫助我,非常感謝。

回答

1

問題是,當下載WSDL文件時,PHP會忽略您的流上下文。一種解決方法是下載WSDL文件,所有的架構進口本地文件系統(我使用tidy這裏打印這個XML):

wget --secure-protocol=SSLv3 https://winsystemsintl.com:54904/PSAService.svc?wsdl --no-check-certificate -O - | tidy -xml -indent > PSAService.svc?wsdl 
wget --secure-protocol=SSLv3 https://winsystemsintl.com:54904/PSAService.svc?xsd=xsd0 --no-check-certificate -O - | tidy -xml -indent > PSAService.svc?xsd=xsd0 
wget --secure-protocol=SSLv3 https://winsystemsintl.com:54904/PSAService.svc?xsd=xsd1 --no-check-certificate -O - | tidy -xml -indent > PSAService.svc?xsd=xsd1 
wget --secure-protocol=SSLv3 https://winsystemsintl.com:54904/PSAService.svc?xsd=xsd2 --no-check-certificate -O - | tidy -xml -indent > PSAService.svc?xsd=xsd2 
wget --secure-protocol=SSLv3 https://winsystemsintl.com:54904/PSAService.svc?xsd=xsd3 --no-check-certificate -O - | tidy -xml -indent > PSAService.svc?xsd=xsd3 

接下來,你必須編輯PSAService.svc?wsdl(文件名wget保存到)並將導入更改爲指向您的本地系統而不是Web。使用更換,所有功能,在您最喜愛的編輯器並更換'https://winsystemsintl.com:54904/'''

前:

<wsdl:types> 
    <xsd:schema targetNamespace="http://tempuri.org/Imports"> 
    <xsd:import schemaLocation="https://winsystemsintl.com:54904/PSAService.svc?xsd=xsd0" 
    namespace="http://tempuri.org/" /> 
    <xsd:import schemaLocation="https://winsystemsintl.com:54904/PSAService.svc?xsd=xsd1" 
    namespace="http://schemas.microsoft.com/2003/10/Serialization/" /> 
    <xsd:import schemaLocation="https://winsystemsintl.com:54904/PSAService.svc?xsd=xsd2" 
    namespace="http://schemas.datacontract.org/2004/07/PSA.Service.MessageObjects.Pregunta" /> 
    <xsd:import schemaLocation="https://winsystemsintl.com:54904/PSAService.svc?xsd=xsd3" 
    namespace="http://schemas.datacontract.org/2004/07/PSA.Service.MessageObjects.Respuesta" /> 
    </xsd:schema> 
</wsdl:types> 

後:

<wsdl:types> 
    <xsd:schema targetNamespace="http://tempuri.org/Imports"> 
    <xsd:import schemaLocation="PSAService.svc?xsd=xsd0" 
    namespace="http://tempuri.org/" /> 
    <xsd:import schemaLocation="PSAService.svc?xsd=xsd1" 
    namespace="http://schemas.microsoft.com/2003/10/Serialization/" /> 
    <xsd:import schemaLocation="PSAService.svc?xsd=xsd2" 
    namespace="http://schemas.datacontract.org/2004/07/PSA.Service.MessageObjects.Pregunta" /> 
    <xsd:import schemaLocation="PSAService.svc?xsd=xsd3" 
    namespace="http://schemas.datacontract.org/2004/07/PSA.Service.MessageObjects.Respuesta" /> 
    </xsd:schema> 
</wsdl:types> 

重複的每個文件被下載。

接下來,你的代碼更改爲以下(我這裏假設所有的PHP/WSDL文件相同的文件夾):

$opts = [ 
'ssl' => [ 
    // set some SSL/TLS specific options 
    'verify_peer' => false, 
    'verify_peer_name' => false, 
    'allow_self_signed' => true 
], 
    'http'=>[ 
    'user_agent' => 'PHPSoapClient' 
    ] 
]; 

// Initialize Soap Client 
$client = new SoapClient('PSAService.svc?wsdl', array('ssl_method' => SOAP_SSL_METHOD_SSLv3,'soap_version' => SOAP_1_2, 'location' => 'https://winsystemsintl.com:54904/PSAService.svc','stream_context' => stream_context_create($opts), 'exceptions' => true, 'trace' => true)); 
var_dump($client->__getFunctions()); 

現在SoapClient已經跳過從網上下載WSDL,和你準備開始使用您的流上下文進行調用。

+0

你也準備好面對MITM攻擊。 :/ – miken32

+0

是的,這是真的,我絕對不會在生產環境中這樣做,但如果開發人員需要使用自簽名證書,它對於測試目的很有用。 –

+0

哎呀,我不能編輯我以前的評論,這是評論完整:您好克里斯,非常感謝您的答案,但現在我得到這個錯誤'無法連接到主機'。我認爲這可能是一個Web服務證書的問題,所以我與他們交談,他們會給我生產Web服務的測試憑據來測試它,是的,我可以連接到生產Web服務沒有任何問題...再次非常感謝。 – Nerea