【问题标题】:PHP call to .NET soap webservicePHP 调用 .NET 肥皂网络服务
【发布时间】:2014-01-10 13:32:00
【问题描述】:

我知道有很多关于 php 调用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 文档。我可以从我的服务中获取功能列表,并且可以将一些数据发送到网络服务。但是,我的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


    【解决方案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());
    

    【讨论】:

    • 我将 https 更改为 http,但它仍然无法正常工作。是的,我在 PHP 中遇到错误。它是荷兰语,所以我会尝试翻译它: 发生错误:尝试发送消息时格式化程序出现异常。反序列化请求方法 AddNumbers 的消息时发生错误。命名空间的最终元素主体:schemas.xmlsoap.org/soap/envelope
    • 没错。但是有两个论点,它仍然不起作用。同样的错误。
    猜你喜欢
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多