2014-01-10 141 views
0

我知道有很多關於PHP調用soap webservice的問題和教程,但我不明白它是如何爲我工作的。PHP調用.NET soap web服務

我用一些非常簡單的方法制作了.NET Windows Communication Foundation Web服務。

我用這個教程:http://www.c-sharpcorner.com/UploadFile/dhananjaycoder/four-steps-to-create-first-wcf-service-for-beginners/

我想從PHP調用此方法。我用一行代碼編寫了一個php文檔。我可以從我的服務中獲取功能列表,並且可以將一些數據發送到Web服務。但是,我的web服務的方法中的參數是空的,但仍然觸發斷點,返回值將返回給我的PHP!

我想我錯過了很多關於標題,參數,安全和更多類似的東西。有人能告訴我我錯過了什麼,我該怎麼做才能得到它?

我的ServiceContract:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.Text; 

namespace Service 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ICalculator" in both code and config file together. 
    [ServiceContract] 
    public interface ICalculator 
    { 

     [OperationContract] 
     double AddNumbers(double number1, double number2); 

     [OperationContract] 
     double SubstractNumbers(double number1, double number2); 

     [OperationContract] 
     double MultiplyNumbers(double number1, double number2); 

     [OperationContract] 
     double DivisionNumbers(double number1, double number2); 
    } 
} 

我的服務:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.Text; 

namespace Service 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Calculator" in code, svc and config file together. 
    // NOTE: In order to launch WCF Test Client for testing this service, please select Calculator.svc or Calculator.svc.cs at the Solution Explorer and start debugging. 
    public class Calculator : ICalculator 
    { 
     public double AddNumbers(double number1, double number2) 
     { 
      double result = number1 + number2; 
      return result; 
     } 

     public double SubstractNumbers(double number1, double number2) 
     { 
      double result = number1 - number2; 
      return result; 
     } 

     public double MultiplyNumbers(double number1, double number2) 
     { 
      double result = number1 * number2; 
      return result; 
     } 

     public double DivisionNumbers(double number1, double number2) 
     { 
      double result = number1/number2; 
      return result; 
     } 
    } 
} 

的Web.Config

<?xml version="1.0"?> 
<configuration> 

    <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5"/> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, set the values below to false before deployment --> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <services> 
     <service name="Service.Calculator"> 
     <endpoint address="" contract="Service.ICalculator" binding="basicHttpBinding"/> 
     <endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding"/> 
     </service> 
    </services> 
    <protocolMapping> 
     <add binding="basicHttpsBinding" scheme="https" /> 
    </protocolMapping>  
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <!-- 
     To browse web app root directory during debugging, set the value below to true. 
     Set to false before deployment to avoid disclosing web app folder information. 
     --> 
    <directoryBrowse enabled="true"/> 
    </system.webServer> 

</configuration> 

PHP

<?php 

try{ 
    echo '<pre>'; 

    $client = new SoapClient('http://localhost:1336/Calculator.svc?wsdl'); 
    print_r($client->AddNumbers(12,21)); 

}catch(Exception $e){ 
    echo 'Exception: '. $e->getMessage() .'\n'; 
} 
?> 

回答

1

你WebConfig文件指定一個HTTPS協議:

<protocolMapping> 
    <add binding="basicHttpsBinding" scheme="https" /> 
</protocolMapping> 

和PHP嘗試在HTTP連接:

$client = new SoapClient('http://localhost:1336/Calculator.svc?wsdl'); 

嘗試改變一個或另一個使它們匹配。
首選HTTP進行測試以避免潛在的證書問題。


此外,AddNumbers方法期望收到兩個參數。

double AddNumbers(double number1, double number2); 

PHP調用中不提供這些提示。

print_r($client->AddNumbers()); 
+0

我將https更改爲http,但仍然無效。是的,我在PHP中遇到錯誤。這是在荷蘭語,所以我會嘗試翻譯它: 發生錯誤:格式化程序中通過嘗試發送消息的例外。通過反序列化消息以請求方法AddNumbers發生錯誤。命名空間的最終元素主體預期:http://schemas.xmlsoap.org/soap/envelope/ – Marten

+0

@MartenB:根據此錯誤編輯答案。 –

+0

那就對了。但有兩個論點,它仍然不起作用。同樣的錯誤。 – Marten