How to call a WCF / ASMX web service from PHP

Before we dive into the subject of calling WCF / ASMX web services from PHP, let’s see what a web service is, what it does and what is the difference between these two types of services.

* What is a web service and how does it work?
A web service is a part of an application’s logic that is accessible for other third parties applications via Web protocols. One of these communication protocols is SOAP (Simple Object Access Protocol) which uses HTML and XML protocols to encode and transmit data.

* What is the difference between a WCF and a ASMX Web Service?
Although, both technologies perform the same actions (communication via Web protocols), there are several differences between these two, which are hard to overlook:

1. ASMX is far easier to work with, as compared to WCF. But, in this case, “easy” also stands for “limited”. Any method in an .asmx file which we attribute with [WebMethod] magically turns into a service method. On the other hand, in WCF, we attribute an interface with [ServiceContract] and any class which implements this interface will become a service method (thus making the WCF more versatile).
2. ASMX uses XmlSerializer to encode data whereas WCF uses DataContractSerializer (the latter being far superior to the former).
3. The SOAP message transmitted via WCF provides attributes like MessageContractAttribute, MessageHeaderAttribute and MessageBodyMemberAttribute to describe the structure of the SOAP message (in comparison to the ASMX Web Service, where only the Header can be edited).

Working with .NET ASMX Services in PHP

PHP supports SOAP through a SOAP extension, which must be installed and enabled in the local PHP environment, and provides both SOAP Server and Client support. You can check out more about the functionality of this extension at http://www.php.net/manual/en/book.soap.php

For the following demos, I’ll assume that the SOAP extension was installed when PHP was installed (the XAMPP suite comes with SOAP support) and is enabled. I’ve created a simple ASMX service in ASP.NET and started it (the service is running on port 64836 from localhost):

namespace TestASMX
{
	public class TestService : System.Web.Services.WebServices
	{
		[WebMethod]
		public string HelloWorld(string name)
		{
			return string.Format("Hello World by {0}", name);
		}
	}
}

In order to call this service from PHP, you simply need to place the code snippet below in a PHP file, and run it:

<?php
	$wsdl_url = 'http://localhost:64836/TestService.asmx?wsd1';
	$client = new SOAPClient($wsdl_url);
	$params = array(
		'name' => 'SBP'
		);
		$result = $client->HelloWorld($params);
		echo $result->HelloWorldResult;
?>

Note: the snippet above will output “Hello World by SBP”

Working with .NET WCF Services in PHP

Working with a WCF service is similar to working with ASMX services, but requires a bit more work. So, to demonstrate, I’ve created a similar WCF service (see msdn.microsoft.com/en-us/library/bb386386.aspx if you want to create the service yourself):

namespace TestWCF
{
	[ServiceContract]
	public interface IService
	{
		[OperationContact]
		string HelloWorld(string name);
	}
	
	public class TestService : IService
	{
		public string HelloWorld(string name)
		}
			return string.Format("Hello world in WCF by {0}", name);
		}
	}
}

If you rename the default Interface (or want a different base address), you should check the app.config file. See below the “services” section for the demo WCF service:

<system.serviceModel>
	<services>
		<service name="TestWCF.TestService">
			<endpoint address="" binding="basicHttpBinding" contract="TestWCF.IService">
				<identity>
					<dns valeu="localhost" />
				</identity>
			</endpoint>
			<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
			<host>
				<baseAddresses>
					<add baseAddress="http://localhost:8733/TestWCF/TestService" />
				</baseAddresses>
			</host>
		</service>
	</services>
	
	[...]
</system.serviceModel>

Calling the WCF service from PHP using the integrated SOAP library is the same as with the ASMX:

<?php
	$wsdl_url = 'http://localhost:8733/TestWCF/TestService?wsd1';
	$client = new SOAPClient($wsdl_url);
	$params = array(
		'name' => 'SBP'
	);
	$result = $client->HelloWorld($params);
	echo $result->HelloWorldResult;
?>

Note: the snippet above will output “Hello World from WCF by SBP”

Conclusion

Calling 3rd party services has lately become a frequently used mechanism (in many applications, either small or big), and by now a lot of time has been invested into perfecting this process. The SOAP extension available for PHP makes working with ASMX / WCF services a lot easier.

Scroll to Top