Calling a WCF service from PHP

The WCF service needs to use basicHttpBinding.

The PHP code:

define('NEWLINE', "<br />\n");

// SOAP client

$wsdl = 'http://example.com/SampleWcfService.SampleService.Service?wsdl';
$soapClient = new SoapClient($wsdl, array('cache_wsdl' => 0));

// SOAP call

$sampleData->SampleProperty = "abc";

$parameters->sampleId = 123;
$parameters->sampleData = $sampleData;

try
{
	$result = $soapClient->GetData($parameters);
}
catch (SoapFault $fault)
{
	echo "Fault code: {$fault->faultcode}" . NEWLINE;
	echo "Fault string: {$fault->faultstring}" . NEWLINE;
	if ($soapClient != null)
	{
		$soapClient = null;
	}
	exit();
}
$soapClient = null;

//echo "<pre>\n";
//print_r($result);
//echo "</pre>\n";

echo "Return value: {$result->GetDataResult}" . NEWLINE;

IService.cs:

using System.ServiceModel;

namespace SampleWcfService.SampleService
{
    [ServiceContract(Namespace = "http://example.com/SampleWcfService/SampleService")]
    public interface IService
    {
        [OperationContract(Name = "GetData")]
        string GetData(int sampleId, SampleType sampleData);
    }
}

Service.cs:

namespace SampleWcfService.SampleService
{
    public class Service : IService
    {
        public string GetData(int sampleId, SampleType sampleData)
        {
            return string.Format("sampleId = {0}, sampleType.SampleProperty = {1}", sampleId, sampleData.SampleProperty);
        }
    }
}

SampleType.cs:

using System.Runtime.Serialization;

namespace SampleWcfService.SampleService
{
    [DataContract(Namespace = "http://example.com/SampleWcfService/SampleService", Name = "SampleType")]
    public class SampleType
    {
        [DataMember(Name = "SampleProperty")]
        public string SampleProperty { get; set; }
    }
}

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<system.serviceModel>
		<services>
			<service
				behaviorConfiguration="SampleWcfService.SampleService.Service.Behavior"
				name="SampleWcfService.SampleService.Service">
				<endpoint address=""
						  binding="basicHttpBinding"
						  contract="SampleWcfService.SampleService.IService" />
				<host>
					<baseAddresses>
						<add baseAddress="http://example.com/SampleWcfService.SampleService.Service" />
					</baseAddresses>
				</host>
			</service>
		</services>
		<behaviors>
			<serviceBehaviors>
				<behavior name="SampleWcfService.SampleService.Service.Behavior" >
					<serviceMetadata httpGetEnabled="true" />
				</behavior>
			</serviceBehaviors>
		</behaviors>
	</system.serviceModel>
</configuration>

8 April 2010 | .NET, PHP | Comments

One Response to “Calling a WCF service from PHP”

  1. 1 dirn 14 April 2010 @ 12:05 am

    wooaa bro!!! sudah main dgn WCF…

Comments:

  1.  
  2.  
  3.