Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

Blog Directory
Blogging Fusion Blog Directory
Web Directory
Blog Directory
EatonWeb Blog Directory
GeekySpeaky: Submit Your Site!
Blog Directory
blogarama - the blog directory
Bloglisting.net - The internets fastest growing blog directory
Blogio.net blog directory
Free Blog Directory
blog search directory
Software Blogs
RSSMicro FeedRank Results
On our way to 1,000,000 rss feeds - millionrss.com
Listed in LS Blogs the Blog Directory and Blog Search Engine
blog directory
Link With Us - Web Directory
Web Blogs Directory Add Your Blog.com

Certificates

Links

Social

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.


<?php
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.

Comments

Gabest said:

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.

# October 2, 2007 4:56 AM

DigiMortal said:

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.

# October 6, 2007 10:36 AM

Lincy said:

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

<?php

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

echo  "Math Test ";

$client = new SoapClient('http://aditidt722.aditi.tech/MathService/Service.svc?wsdl');

$obj->value = 2;

$obj->value1 = 3;

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

echo "The Result is : ";

?>

Can you help me with this????

# April 8, 2008 7:34 AM

DigiMortal said:

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.

# April 8, 2008 8:21 AM

Lincy said:

PHP 5, N yes the built in SOAP support

# April 8, 2008 10:42 AM

Wade said:

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?

# May 8, 2008 4:06 AM

DigiMortal said:

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.

# May 8, 2008 4:43 AM

Wade said:

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.

# May 8, 2008 9:36 PM

Wade said:

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("192.168.1.151/.../Service1("location" => '192.168.1.151/.../Basic'));

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

echo($result->GetDataResult);

# May 8, 2008 9:44 PM

Stefan said:

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://192.168.../ETS/bas'));

# May 13, 2008 11:19 AM

Wade said:

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

# May 14, 2008 12:36 AM

Banu said:

Hi,

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(

"secure.dmsintegration.com/.../FinanceService.svc,

array(

'soap_version' => SOAP_1_1,

'trace' => 1,

'exceptions' => 1

));

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

array('request' => $params),null);

Any suggestions?

Thanks,

Banu

# May 21, 2008 4:54 PM

DigiMortal said:

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

# May 21, 2008 5:45 PM

Banu said:

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

Thanks,

Banu

# May 21, 2008 7:42 PM

DigiMortal said:

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.

# May 22, 2008 2:05 AM

Banu said:

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.

Banu

# May 22, 2008 9:46 AM

DigiMortal said:

I really don't understand what and how you are doing and how is this related to WCF :)

# May 22, 2008 9:54 AM

Banu said:

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(

"secure.dmsintegration.com/.../FinanceService.svc,

array(

'soap_version' => SOAP_1_1,

'trace' => 1,

'exceptions' => 1,

'$location' => 'secure.dmsintegration.com/.../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.

Thanks,

Banu

# May 22, 2008 10:00 AM

Banu said:

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(

"secure.dmsintegration.com/.../FinanceService.svc,

array(

'soap_version' => SOAP_1_1,

'trace' => 1,

'exceptions' => 0,

'$location' => 'secure.dmsintegration.com/.../legacy'

));

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.

Thanks,

Banu

# May 22, 2008 10:09 AM

DigiMortal said:

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

# May 22, 2008 2:45 PM

Andrey said:

$client->soap_defencoding = 'UTF-8';

# July 22, 2008 10:59 PM

Yuval said:

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?

Thanks

# September 4, 2008 1:47 AM

Matt Williamson said:

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(

 array(

   'x' => 2.5,

   'y' => 3.5

 )

);

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

# September 15, 2008 8:03 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)