0

I know there are a lot of questions and tutorials about a php call to a soap webservice, but I don't understand how it works for me.

I have made a .NET Windows Communication Foundation Webservice with a couple of very easy methods.

I used this tutorial: http://www.c-sharpcorner.com/UploadFile/dhananjaycoder/four-steps-to-create-first-wcf-service-for-beginners/

I want to call this methods from PHP. I have made a php document with single lines of code. I can get the list of functions from my service and i can send some data to the webservice. But, the parameters in the methods of my webservice are empty but still the breakpoint is triggered and the return value will be returned to my PHP!

I guess that I miss a lot of things about header, parameters, security and more stuff like that. Can someone tell me what I miss and what I should do to get it?

My 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);
    }
}

My Service:

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 Answer 1

1

Your WebConfig file specifies a HTTPS protocol :

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

And PHP attempts a connection on HTTP :

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

Try changing one or another so they match.
Prefer HTTP for testing to avoid a potential certificate problems.


Also, the AddNumbers method expect to receive two arguments.

double AddNumbers(double number1, double number2);

Theses aren't provided in the PHP call.

print_r($client->AddNumbers());
Sign up to request clarification or add additional context in comments.

2 Comments

I changed https to http but it still don't works. Yes, i get an error in PHP. It's in dutch, so I will try to translate it: An error occured: an exception in the formatter by trying to send the message. An error occured by deserializing the message for request the method AddNumbers. Final element body of namespace expected: schemas.xmlsoap.org/soap/envelope
Thats right. But with two arguments, it still don't work. The same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.