tags:

views:

879

answers:

3

I would like to call C# service with using PHP, anyone know how to do it? Thanks

+4  A: 

Create an SOAP XML document that matches up with the WSDL and send it via HTTP POST. See here for an example.

You send this:

POST /webservices/tempconvert.asmx HTTP/1.1
Host: www.w3schools.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"&gt;
  <soap12:Body>
    <CelsiusToFahrenheit xmlns="http://tempuri.org/"&gt;
      <Celsius>string</Celsius>
    </CelsiusToFahrenheit>
  </soap12:Body>
</soap12:Envelope>

And get this back:

HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"&gt;
  <soap12:Body>
    <CelsiusToFahrenheitResponse xmlns="http://tempuri.org/"&gt;
      <CelsiusToFahrenheitResult>string</CelsiusToFahrenheitResult>
    </CelsiusToFahrenheitResponse>
  </soap12:Body>
</soap12:Envelope>
Eclipse
Hi Josh, How about if I wish to get a report file(in pdf formet) from the web service to php, is saop still the best way to used?
Jin Yong
@Jin: you can send binary directly in the returned XML by encoding it with UUEncode or base64. A better plan would be to generate the PDF on the server, give it a unique URL and then the client can get the report in two requests - one SOAP request to generate the report followed by a plain http request to retrieve the report.
Eclipse
A: 

I've never done it in PHP->C#, I've used C# to call PHP webservices and I've always used the Zend Framework wrapper for native PHP classes. You should check out the Zend_Soap_Client which, if it's like the Zend_Soap_Server, is just a wrapper with some value added stuff around the PHP SOAP classes.

And, I should say, all it does is wrap up what @Josh said into a nice class and does some things automatically for you.

Mark G
A: 

There's no thing called a "C# Web Service". What you mean is an XML Web Service based on SOAP remote calls and WSDL for description.

Indeed all SOAP services are supposed to be inter compatible, be it .Net, PHP or Java. But in practice, minor problems make it harder.

There are many different SOAP libraries for PHP, but for connection to an ASP.NET XML Web Service from PHP, but nuSOAP gave the best results for me. It's basicly a set of PHP-classes to consume SOAP-based web services. The simplest client code seems like this:

<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the client instance
$client = new soapclient('http://localhost/phphack/helloworld.php');
// Call the SOAP method
$result = $client->call('hello', array('name' => 'Scott'));
// Display the result
print_r($result);
?>

See http://www.scottnichol.com/nusoapintro.htm for more examples.

Gorkem Pacaci
The example is taken from the url given. It connects to a php-hosted web service, but connection to a .net-based service isn't different.
Gorkem Pacaci