PHP calling .NET – PHP to WCF calls with parameters

In my last post I provided an example of PHP calling a .NET windows communication foundation web service. The PHP invoked the .NET service with no parameters, getting the time on server.

Sometime (well, most of the times) you need to pass parameters to the .NET web service. for example you might want to pass a client ID and get back its account balance.

 

Here is what you do:

First create a "out of the box" windows communication foundation (WCF) WCF service library project ( I used Visual Studio 2008) .

Then, modify the project files according to the files posted bellow. 

My server side WCF code looks like this:

1)  Service1.cs –

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

namespace WcfServiceLibrary2
{
    // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in App.config.
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

    }
}

2)  IService1.cs

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

namespace WcfServiceLibrary2
{
    // NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in App.config.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);

    }   
}

The server side WCF application configuration (App.config) file looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <!– When deploying the service library project, the content of the config file must be added to the host’s
  app.config file. System.Configuration does not support config files for libraries. –>
  <system.serviceModel>
    <services>
      <service name="WcfServiceLibrary2.Service1" behaviorConfiguration="WcfServiceLibrary2.Service1Behavior">
        <host>
          <baseAddresses>
            <add baseAddress = "
http://localhost:8731/Design_Time_Addresses/WcfServiceLibrary2/Service1/" />
          </baseAddresses>
        </host>
        <!– Service Endpoints –>
        <!– Unless fully qualified, address is relative to base address supplied above –>
        <endpoint address ="" binding="basicHttpBinding" contract="WcfServiceLibrary2.IService1">
          <!–
              Upon deployment, the following identity element should be removed or replaced to reflect the
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity
              automatically.
          –>
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <!– Metadata Endpoints –>
        <!– The Metadata Exchange endpoint is used by the service to describe itself to clients. –>
        <!– This endpoint does not use a secure binding and should be secured or removed before deployment –>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfServiceLibrary2.Service1Behavior">
          <!– To avoid disclosing metadata information,
          set the value below to false and remove the metadata endpoint above before deployment –>
          <serviceMetadata httpGetEnabled="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>
  </system.serviceModel>
</configuration>

 

Note my blog post about the importance of setting this WCF service binding to basicHttpBinding

The PHP side of the application code looks like this:

<h1>
<?php

try{
    $client = new
        SoapClient(
            "
http://localhost:8731/Design_Time_Addresses/WcfServiceLibrary2/Service1/?wsdl"
        );
    $params = array(‘value’=>"3");
    $webService = $client->GetData($params);
    $wsResult = $webService->GetDataResult;
    print  $wsResult;
} catch (Exception $e) {
    print  ‘Caught exception: ‘.  $e->getMessage(). "\n";
}

?>

</h1>

 

This is very similar to the previous example of PHP to WCF communication, only here the PHP passes an array of parameters to the WCF web service:

$params = array(‘value’=>"3");
$webService = $client->GetData($params);

In my next post I will show how to investigate the WCF service and learn the parameters it requires and their soup names.


Share the love...Tweet about this on TwitterShare on LinkedInShare on Google+Share on Facebook

Amir Shevat

Amir Shevat is the global Startup Outreach lead in Google Developer Relations (g.co/launch). Previously, Amir Led Google Campus Tel Aviv and was the co founder of several startups.

You may also like...

8 Responses

  1. ZEMZEMI says:

    Hi,
    Thank you for your post.
    I have the same problem like the gay who has a DataTime parameter and I would like to know how can I pass it from php.

    Best regards

    • Amir Shevat says:

      @ZEMZEMI – I am now touring the world but in two weeks I will be back and will create an example with DataTime. Sorry for the delay..

  2. Gareth says:

    Hi Amir,

    I am relatively new to the world of PHP and have been tasked to consume a WCF web service. I have a form that is to collect a user’s name, email address and mobile number and pass these values to the web service so that it can send a SMS to the user however I have no idea what I have to do. The vendor has provided me with documentation however it makes no sense to me. Do you think you might be able to assist?

  3. anonymous says:

    Hi,

    I have a .Net webservice called GetDateTime() which accepts DateTime object as a parameter. I have php client that calls this webservice. I would like to know how pass a DateTime object from PHP to the webservice.

    I tried something like below:
    $dateTime1 = new DateTime(“now”, new DateTimeZone(‘UTC’));
    var_export($dateTime1);
    $d = $client->GetDateTime($dateTime1);
    echo $d->GetDateTimeResult;

    But the result is alwa 1/1/0001 12:00:00 , no matter what the variable $datetime1 is.

    Any help?

  4. Amir Shevat says:

    Can you please post the WCF service side?

    Cheers
    Amir Shevat

  5. anonymous says:

    Cs function:

    Check_Info(SysEnumInfos.Info info, string ID)

    PHP:

    $isGPara = array(“info” => “info.ab”, “ID” => “sfwrw33”);
    $webService = $client->Check_Info($isGPara);

    is it correct about “info” => “info.ab” ?

Leave a Reply

Your email address will not be published. Required fields are marked *