【发布时间】:2014-01-10 13:32:00
【问题描述】:
我知道有很多关于 php 调用soap web 服务的问题和教程,但我不明白它是如何为我工作的。
我用几个非常简单的方法创建了一个 .NET Windows Communication Foundation Web 服务。
我想从 PHP 中调用这些方法。我用单行代码制作了一个 php 文档。我可以从我的服务中获取功能列表,并且可以将一些数据发送到网络服务。但是,我的webservice的方法中的参数是空的,但仍然触发断点,返回值将返回给我的PHP!
我想我错过了很多关于标题、参数、安全性和更多类似的东西。谁能告诉我我想念什么以及我应该怎么做才能得到它?
我的服务合同:
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';
}
?>
【问题讨论】:
标签: c# php wcf web-services soap