Using WCF Services with PHP

I recently wrote two postings on the subject of WCF services: WCF Services – Let’s Get Started and WCF and Unit Tests. One could see that creating and testing services was not complicated at all. Let’s go on and take some steps nearer to the real world – our service is used by SOAP clients running on different platforms.

I conducted my first experiment with PHP. PHP is widely used in the world of the Web and in all likelihood there will dawn a day when DT is asked just for such an integration. As PHP4 development and support is soon going to be discontinued, I didn’t bother with an example using PHP4 SOAP tools. PHP5 is on the market and it comes with a quick and stable SOAP library. The performance is many times better than in case of similar widgets written for PHP4.

I have Apache and PHP5 running on Windows and for SOAP support I have to use an extension named php_soap.dll. Thus, I have to remove the comment mark in front of the appropriate php.ini line and restart Apache.

As an example I will present a PHP code calling the service’s Add() method and writing the result on the screen. An example of WCF code can be found in our blog, in the posting WCF Services – Let’s Get Started.

header('Content-Type: text/plain'); echo "WCF Test\r\n\r\n"; $client = new SoapClient('http://gamma/wcftest/MathService.svc?wsdl');
$obj->x = 2.5;
$obj->y = 3.5;
$retval = $client->Add($obj); echo "2.5 + 3.5 = " . $retval->AddResult;

If nothing went wrong, the result should be something like this:

    WCF Test

    2.5 + 3.5 = 6

As you can see, using WCF services in PHP is very simple. The SOAP library provided along with PHP5 does a great deal of the work "behind the curtains", leaving us with an easily readable brief code.

See also

45 thoughts on “Using WCF Services with PHP

  • Gabest says:

    Packing arguments into undeclared objects… well, isn't there a better way? If php runs on Windows then I found it much more convenient to generate a proxy class library dll and use that through COM, but that's not always the case, currently I have to make a call from linux and that's how I found your article googling for a solution.

  • Gunnar says:

    Thanks you for your reply. I have to admin that undeclared object is not a best solution. It is really better to create a class for arguments object.

  • Lincy says:

    I followed the above steps. Created a WCF service and hosted on IIS on a different machine and trying to access that service in the PHP snippet. But gettign teh following error.

    Math Test

    Fatal error: Uncaught SoapFault exception: [HTTP] Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'. in C:\Documents and Settings\lincyc\My Documents\PHPDocument1.php:10 Stack trace: #0 [internal function]: SoapClient->__doRequest('<?xml version="…', 'http://aditidt7…', 'http://tempuri….', 1, 0) #1 [internal function]: SoapClient->__call('Add', Array) #2 C:\Documents and Settings\lincyc\My Documents\PHPDocument1.php(10): SoapClient->Add(Object(stdClass)) #3 D:\Program Files\Zend\ZendStudio-5.5.1\bin\php5\dummy.php(1): include('C:\Documents an…') #4 {main} thrown in C:\Documents and Settings\lincyc\My Documents\PHPDocument1.php on line 10

    The php code is


    header('Content-Type: text/plain');

    echo  "Math Test ";

    $client = new SoapClient('');

    $obj->value = 2;

    $obj->value1 = 3;

    $retval = $client->Add($obj);

    echo "The Result is : ";


    Can you help me with this????

  • Gunnar says:

    Which version of PHP you are using? If it is PHP5, do you use built-in SOAP support? If you can provide me this information it is easier for me to help you.

  • Lincy says:

    PHP 5, N yes the built in SOAP support

  • Wade says:

    Getting this error when calling method "Fatal error: Uncaught SoapFault exception: [HTTP] Cannot process the message because the content type ‘text/xml; charset=utf-8’ was not the expected type ‘application/soap+xml; charset=utf-8’."

    PHP Version => 5.2.3-1 (failed on both cli and apache)

    PHP Box => ubuntu 6.3

    ISS Box => Vista 64 ult

    Using Soap which is compiled in.

    Any ideas?

  • Gunnar says:

    I think you have problem with SOAP version. Check out what SOAP version uses WCF and which one is used by PHP. Then make sure that they both will use same version.

  • Wade says:

    Ok so now I have both endpoints. I had to tell the soapclient to use a different location to the advertised wdsl to get it to work. Although I admit my web service skills are lacking, so I’m not sure if this is a default requirement with PHP or that I’ve miss configured the endpoints.

    $soap = new soapclient("…/Service1("location" => ‘…/Basic‘));

    $result = $soap->GetData(6);


  • Stefan says:

    it is not very clear from above example:

    It got to work for me when I used:

    $client = new soapclient("http://192.168…/ETaskS/?wsdl", array("location"=>’http

  • Wade says:

    Yeah that’s what I used. The array declaration was gobbled by the url parsing code.

  • Banu says:


    I am using a soap call in my PHP and I get the same error

    Fatal error: Uncaught SoapFault exception: [HTTP] Cannot process the message because the content type ‘text/xml; charset=utf-8’ was not the expected type ‘application/soap+xml; charset=utf-8’

    I am using XAMP in my localbox and using PHP 5 in XAMP

    The call that I’m using is

    $client = new SoapClient(



    ‘soap_version’ => SOAP_1_1,

    ‘trace’ => 1,

    ‘exceptions’ => 1


    $result = $client->__soapCall(‘CreateFinanceDeal’,

    array(‘request’ => $params),null);

    Any suggestions?



  • Gunnar says:

    Make sure that WCF and PHP app will use same SOAP protocol version.

  • Banu says:

    What is WCF? Sorry i am new to web services.



  • Gunnar says:

    WCF stands for Windows Communications Foundation. It is technology to build services and it was shipped with .Net Framework 3.0. Now you can build stand-alone services. Previously you were bind to ASP.NET and Web Service project that offered only SOAP. With WCF you can also use the other standard for web services.

  • Banu says:

    But I dont use .net framework. I use windows XP Professional ver 5.1 and use XAMPP for windows version 1.6 and soap client and version are both enabled in my xampp and use php 5.2.5.


  • Gunnar says:

    I really don’t understand what and how you are doing and how is this related to WCF 🙂

  • Banu says:

    I use XAMPP and PHP on my localbox and soap in enabled in php and use apache server on xampp. All I'm trying to do is do a soap call and get the result xml back.

    here is the soap wsdl for which i create a new soap client

    $client = new SoapClient(



    'soap_version' => SOAP_1_1,

    'trace' => 1,

    'exceptions' => 1,

    '$location' => '…/legacy'


    then I send the data, uernameand passwd along with the call

    $result = $client->__soapCall('CreateFinanceDeal',

    array('request' => $params),

      array('soapaction' => 'urn:RyanTech.DmsIntegration.PartnerApi.Finance/DmsiPartnerApiFinanceService/CreateFinanceDeal')


    Does it makes sense. Please let me know if you have any furthur questions. I'm really stuck at this point as to what this error means

    Fatal error: Uncaught SoapFault exception: [HTTP] Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'.

    Any help will be appreciated.



  • Banu says:

    May be I'm at the wrong place asking for help. I posted here as the error was very similar to mine. I use PHP and Apache in XAMPP for windows and soap is enabled and get the following error

    SoapFault exception: [HTTP] Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'

    The wsdl to which i create a soap client and make a soap call to is

    $client = new SoapClient(



    'soap_version' => SOAP_1_1,

    'trace' => 1,

    'exceptions' => 0,

    '$location' => '…/legacy'

    p> ));

    The data and the credentials are passed in this

    $result = $client->__soapCall('CreateFinanceDeal',

    array('request' => $params),

      array('soapaction' => 'urn:RyanTech.DmsIntegration.PartnerApi.Finance/DmsiPartnerApiFinanceService/CreateFinanceDeal')


    Any help will be appreciated. There is not much help around for this error.



  • Gunnar says:

    Hmm…. svc extension points to WCF. Try to change SOAP version in your PHP request. Instead of

    ‘soap_version’ => SOAP_1_1

    try out

    ‘soap_version’ => SOAP_1_2

  • Andrey says:

    $client->soap_defencoding = ‘UTF-8’;

  • Yuval says:

    I see there are some comment about WSHttpBinding but it is not clear how to use it.

    Can you please explain what should I do in order to consume WCF service with WSHttpBinding from PHP

    Is it possible to do it from a computer outside of the domain?


  • You can use arrays to specify an object. This makes complex objects very simple. PHP will cast all arguments to the correct type automatically. Here’s your sample with the modification:

    header(‘Content-Type: text/plain’);

    echo "WCF Test\r\n\r\n";

    $client = new SoapClient(‘gamma/…/MathService.svc‘);

    $retval = $client->Add(


       ‘x’ => 2.5,

       ‘y’ => 3.5



    echo "2.5 + 3.5 = " . $retval->AddResult;

  • Kiemieuk says:

    For the Banu problem,

    i think it caused of banu put the service url in svc extension.

    For .Net i think its okey buts for PHP,

    it should put .svc/wsdl? after the .svc extension

    Maybe its can solver the problem.

    Buts thank to the blog owner, your blog really teaching me to doin well in my WCF

  • Gunnar says:

    Thanks for good words, Kiemieuk 🙂

  • xac says:

    hi all,

    maybe you can help me, because i am out of any idea – sorry, my english is not very well.

    i build some webService the same way you described it on this page and it works 🙂

    now, for a new client webservice i need to send an authentification too, and it doesn't works 🙁 i tried examples from diifferent sites and it seems to be very complicated. with fsockopen and a handmade xml-file the response works, but we should  realise it with SOAP because of https and performance.


    $param = array("pass"=>$pw, "user"=>$user);

    $res = $client->__soapCall("GetInfo",  array('parameters' => $param), null);

    this xml will be send

    <?xml version="1.0" encoding="UTF-8"?>

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="…/" xmlns:ns1="MioService"><SOAP-ENV:Body><ns1:GetInfo/></SOAP-ENV:Body></SOAP-ENV:Envelope>

    first problem: the params are missing…

    second problem: i have to set some xml before the body:


    <SOAP-ENV:Header><h:Context xmlns:h="; xmlns:i="…/XMLSchema-instance"><DataID xmlns="http://url">$dataID</DataID></h:Context></SOAP-ENV:Header>

    so that it looks like:

    <?xml version="1.0" encoding="UTF-8"?>

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="…/" xmlns:ns1="MioService"><SOAP-ENV:Header><h:Context xmlns:h="; xmlns:i="…/XMLSchema-instance"><DataID xmlns="http://url">$dataID</DataID></h:Context></SOAP-ENV:Header><SOAP-ENV:Body><ns1:GetInfo/></SOAP-ENV:Body></SOAP-ENV:Envelope>

    and the third problem is, that the webservice does not accept the webservice-name in the envelope-tag [ xmlns:ns1="MioService"] is there a way to delete this?

    thanx 4 help or any idea


  • xac says:

    hi all again,

    i wished, you don't publish my questions 😉

    ok, a long sunday, no telephone rings and the soap tracing was on, i tried so many little changes, and i got it :-))

    after i tried different SoapClient calls, the wsdl call was the better way, then setting location and uri manually.

    $wsdl = "http://uri?wsdl&quot;;

    $connect = array('soap_version' => SOAP_1_1, 'trace' => 1, 'exceptions' => 0);

    $client  = new SoapClient($wsdl, $connect);

    then i had to build the header – puh, spending a lot of hours 😉

    at last, i didn't find an other way, then setting it manually, because the webservice server, wanted to have a specific definition for authentificaton. but how to set an own xml header, SOAP didn't change? => XSD_ANYXML

    $log = '<a:myxml>your xml</a:myxml>';

    $authvalues = new SoapVar($log, XSD_ANYXML);

    $header = new SoapHeader('http://uri/context', 'Context', $authvalues);

    => header looks like this:

    '<ns1:Context><a:myxml>your xml</a:myxml></ns1:Context>'

    and the specific url is set before in the envelope by SOAP as SOAP-ENV:ns1:'http://uri/context'

    now the soapCall – same procedure as all the hours before 😉 testing -> the result was, that i have to set the body by myself, too.

    very important for me was the soapaction parameter and the results of SoapVars!

    without setting, the soapaction, Soap set a wrong url in the header.

    for the body i started to build my own xml again ->

    $request = '<s:mybodyxml>your xml</s:mybodyxml>';

    $param = new SoapVar($request, XSD_ANYXML);

    $funktion = "NameOfFunctionToCall";

    $soapaction = array('uri' => 'WebServiceUri', 'soapaction' => 'WebServiceUri/WebServiceUri/'.$funktion);

    $res = $client->__soapCall($funktion,  array("parameter"=>$param),  $soapaction, $header);

    and now it works fine 🙂

    hope it helps!!!

    happy eastern


  • ciary says:

    hi all,

    i’m pretty new to webservices but i need them for a project to convert a database.

    but since this is new, i first tried to put a simple ‘Hello World!’ on my screen using WCF.

    now when i tried this code (see below) i got a fatal error. i dont know how to solve it. i’ve put the folder with my svc into the home folder of my website.


    my code:


    header(‘Content-Type: text/plain’);

    echo "WCF Test\n";

    $client = new SoapClient(‘webservice try 1/webservice try 1/Service1.svc?wsdl’);

    $retval = $client->Test();

    echo $retval;



    my output:

    WCF Test

    <br />

    <b>Warning</b>:  SoapClient::SoapClient() [<a href=’soapclient.soapclient’>soapclient.soapclient</a>]: I/O warning : failed to load external entity &quot;webservice try 1/webservice try 1/Service1.svc?wsdl&quot; in <b>C:\xampp\htdocs\page\testWCF.php</b> on line <b>6</b><br />

    <br />

    <b>Fatal error</b>:  Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn’t load from ‘webservice try 1/webservice try 1/Service1.svc?wsdl’ : failed to load external entity &quot;webservice try 1/webservice try 1/Service1.svc?wsdl&quot;

    in C:\xampp\htdocs\page\testWCF.php:6

    Stack trace:

    #0 C:\xampp\htdocs\page\testWCF.php(6): SoapClient-&gt;SoapClient(‘webservice try …’)

    #1 {main}

     thrown in <b>C:\xampp\htdocs\page\testWCF.php</b> on line <b>6</b><br />


    any help would be welcome

  • ciary says:

    ok, seems i copied my url wrong. although i encountered a second problem on line 8. ‘Unsupported Media Type’

    does anyone know how i should fix that??

    ty 🙂



  • ses5909 says:

    Thanks for the example, I came across it while looking for WCF API documentation examples. Need to be able to explain to PHP developers how to use the service. If you have any good documentation examples, feel free to list them 🙂


  • Mohammed says:


    I really need your help please

    i keep getting this error don,t know what to do with it

    The SOAP action specified on the message, ”, does not match the HTTP SOAP Action


  • Fernando Mendes says:

    the PHP SoapClient class implements the WS-I standard.  To resolve the HTTP SOAP action mismatch exception:

    Change the binding of the WCF service in the web.config file from binding="wsHttpBinding" to binding="basicHttpBinding".

    Good luck…

  • Wade says:

    Ok problem solved. Changed binding to basicHttp and it worked with PHP.

    My new problem now is using multiple endpoints. I’d like to have a wsHttp and basicHttp. Trying to workout how now.

  • Riaan Bergh says:

    Hi Guys,

    I looking for some help I need to call a wcf service through PHP and from the information I got it is declared as wsHttpBinding I cannot change this as this

    is another companies service.

    Anbody able to help me with this error ?

    /////////////// Error ////////////////////////////

    [02-Jun-2009 08:45:04] PHP Fatal error:  Uncaught SoapFault exception: [s:Sender] The SOAP action specified on the message, '', does not match the HTTP SOAP Action, '…/Authenticate'.  in D:\Lex Legis Code\PHPServiceTester\Test.php:58

    Stack trace:

    #0 [internal function]: SoapClient->__call('Authenticate', Array)

    #1 D:\Lex Legis Code\PHPServiceTester\Test.php(58): SoapClient->Authenticate(Array)

    #2 {main}

     thrown in D:\Lex Legis Code\PHPServiceTester\Test.php on line 58

    //////////////////// Test Code ////////////////


    header('Content-Type: text/plain');

    echo "WCF Test\r\n\r\n";

    echo "Before Constructor of soapClient is called\n";

    $client = new SoapClient('…/InoxicoAdmin.svc', array('soap_version' => SOAP_1_2,'timeout' => 360));

    echo "After Constructor of soapClient is called\n";

    print_r( $client->__getFunctions());

    $request['Authenticate'] = array('Username'=>'a','Password'=>'a','ProductCode'=>'sss');



  • Thank you DigiMortal, it's a great post and helped me a lot.

  • Diego Toala says:

    Thanks a lot!! Great Article , I was using NuSoap and was difficult consume the wcf service, I can’t find actual info about NuSoap, You Rock!

  • Boo Radley says:

    Does anyone know how to pass credentials from a PHP client to a WCF service if said service requires a username and password?

  • Sherry F says:

    Riaan Bergh – how did you resolve the issue?  I’m having the same problem.  The service is not owned by me and I can’t "Change the binding of the WCF service in the web.config file from binding="wsHttpBinding" to binding="basicHttpBinding"."  I have to somehow set in php/nusoap that we need to USE basicHttpBinding.  Is that a setting we can use in nusoap?  

  • Eric says:

    USING the basicHttpBinding bindings & SOAP 1.1, I was able to communicate w/ a WCF service

    $client = new SoapClient("http://localhost/?wsdl&quot;,

     array(‘soap_version’ => SOAP_1_1, ‘trace’ => 1,’exceptions’ => 1 )); //’soap_version’ => SOAP_1_2

    echo("<BR>Dumping client object:<BR>");

    var_dump($client); //dump client objects


    echo("Dumping client functions:<BR>");

    var_dump($client->__getFunctions()); //get available functions


    echo("Dumping list of SOAP types:<BR>");

    var_dump($client->__getTypes()); //get soap data types


    $parameters = array("ZipCode" => "90210"); //parameters to pass to function

    echo("Calling SOAP Add function with params: ");


    echo ("<BR><BR>");

    $result = $client->Temp($parameters); //call function via soap client & get result

    echo("Dumping request headers:<BR>".$client->__getLastRequestHeaders()."<BR><BR>");

    echo("Dumping request:<BR>".htmlspecialchars($client->__getLastRequest())."<BR><BR>");

    echo("Dumping response headers:<BR>".$client->__getLastResponseHeaders()."<BR><BR>");

    echo("Dumping response:<BR>".htmlspecialchars($client->__getLastResponse())."<BR><BR>");

    if( is_soap_fault($result) == true ) { //check if there was a soap error

    //echo "There was a fault.<BR>";

    trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);

    } else {

    echo("Got result: ");



    echo($result->TempResult); //get result value


  • Hayk says:

    Fatal error: Uncaught SoapFault exception: [HTTP] Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'. in C:\wamp\www\test\soap\index.php:19 Stack trace: #0 [internal function]: SoapClient->__doRequest('<?xml version="…', 'http://192.168….', 'http://tempuri…', 1, 0) #1 C:\wamp\www\test\soap\index.php(19): SoapClient->__soapCall('Balance', Array) #2 {main} thrown in C:\wamp\www\test\soap\index.php on line 19

  • noobs says:

    Can i authenticate using a certificate and nuSoap,

    I need to call a .NET Webservice using the given certificate.

    Can anyone please help, i am new to Webservice and do apologize if my question sounds


  • Cloud_kid says:

    Hi,i'm from China.

    Nice to meet you.

    I want to use PHP connect WCF.

    C# code:

    ServiceReference1.StudentInfoClient proxy = new ServiceReference1.StudentInfoClient();

    proxy.ClientCredentials.UserName.UserName = "xx";

    proxy.ClientCredentials.UserName.Password = "xxxx";           Console.Write(proxy.StudentAccountValidate("xxxx", "xxx"));           Console.Write(proxy.GetStudentInfoByStudentNo("xx").StudentName);


    that is C# code.But in PHP,what should I do?my english is poor,but I think you could help me.So…Thank you very much.

  • lonwin says:

    Thanks a lot!! Great Article! help me solved my problem!

  • Chris says:

    Another way to consume WCF service using PHP can be found here…/consuming-wcf-service-with-get-and-post-using-php.html

  • grahn says:

    ‘Fatal error: Uncaught SoapFault exception: [HTTP] Cannot process the message because the content type ‘text/xml; charset=utf-8’ was not the expected type ‘application/soap+xml; charset=utf-8′.’

    I’ve got same issue and found out its because of wsHttpBinding. using basicBinding I resolved that. this blog explain that

  • Goblin Knight says:

    one thing may consider is the setting of SoapClient

    $options = array(

    $client = new SoapClient(“http://localhost/FileUploadService.svc?wsdl”,$options);

Leave a Reply

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