PHP to WCF with SOAP?
Posted: Mon Oct 26, 2009 5:12 pm
I have been tasked with creating SOAP service that is part of an already existing windows service and to communicate with it via SOAP in PHP.
It has been 5 years since I've written PHP and I have only dabbled with .NET. Anyway I googled my ass off and wrote up a windows service that hosts a WCF service and implemented a test contract. Evidently it does not create a WSDL file, or perhaps it makes one itself when its requested. I dunno, the docs suck.
I wrote up a PHP file to test it out and it errors out that it cannot connect.
I get the following output:
PHP Warning: SoapClient::SoapClient(http://localhost/TestService.wsdl) [soapclient.soapclient]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in C:\inetpub\wwwroot\DatabaseUpdate.php on line 80 PHP Warning: SoapClient::SoapClient() [soapclient.soapclient]: I/O warning : failed to load external entity "http://localhost/TestService.wsdl" in C:\inetpub\wwwroot\DatabaseUpdate.php on line 80 PHP Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://localhost/TestService.wsdl' : failed to load external entity "http://localhost/TestService.wsdl" in C:\inetpub\wwwroot\DatabaseUpdate.php on line 80
I don't know how to instantiate a PHP client properly. I don't understand URIs and am pretty much lost.
Any help?
Here is my PHP code. (PS the scroll box here seems to be all screwed up)
Here is my windows service code
It has been 5 years since I've written PHP and I have only dabbled with .NET. Anyway I googled my ass off and wrote up a windows service that hosts a WCF service and implemented a test contract. Evidently it does not create a WSDL file, or perhaps it makes one itself when its requested. I dunno, the docs suck.
I wrote up a PHP file to test it out and it errors out that it cannot connect.
I get the following output:
PHP Warning: SoapClient::SoapClient(http://localhost/TestService.wsdl) [soapclient.soapclient]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in C:\inetpub\wwwroot\DatabaseUpdate.php on line 80 PHP Warning: SoapClient::SoapClient() [soapclient.soapclient]: I/O warning : failed to load external entity "http://localhost/TestService.wsdl" in C:\inetpub\wwwroot\DatabaseUpdate.php on line 80 PHP Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://localhost/TestService.wsdl' : failed to load external entity "http://localhost/TestService.wsdl" in C:\inetpub\wwwroot\DatabaseUpdate.php on line 80
I don't know how to instantiate a PHP client properly. I don't understand URIs and am pretty much lost.
Any help?
Here is my PHP code. (PS the scroll box here seems to be all screwed up)
Code: Select all
<?php
$client = new SoapClient("http://localhost/TestService.wsdl");
echo("\nDumping client object:\n");
var_dump($client);
}
catch(Exception $e)
{
echo 'Caught exception: '. $e->getMessage(). "\n";
}
?>
Code: Select all
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceProcess;
using System.Text;
namespace TestService
{
/// <summary>
/// The hosted service
/// </summary>
public class TestService : ServiceBase, ITestService
{
public ServiceHost serviceHost = null;
/// <summary>
/// Constructor
/// </summary>
public TestService()
{
this.ServiceName = "TestService";
this.CanPauseAndContinue = true;
}
/// <summary>
/// Dispose
/// Cleans up objects that need it
/// </summary>
/// <param name="disposing">Whether or not disposing is going on.</param>
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
/// <summary>
/// Entry point of executable
/// </summary>
public static void Main()
{
ServiceBase.Run(new TestService());
}
/// <summary>
/// Called when the service starts
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args)
{
// Set the base address where the service may be called by clients
this.serviceHost = new ServiceHost(typeof(TestService), new Uri("http://localhost:8000"));
try
{
// Add a service endpoint
// The endpoint binding is appended to the base address set in the service host
serviceHost.AddServiceEndpoint(typeof(ITestService), new BasicHttpBinding(), "TestService");
// Enable metadata exchange
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
serviceHost.Description.Behaviors.Add(behavior);
// Start the service
serviceHost.Open();
}
catch(CommunicationException e)
{
serviceHost.Abort();
}
}
/// <summary>
/// Called when the service stops
/// </summary>
protected override void OnStop()
{
serviceHost.Close();
}
#region IServiceContract Members
public double Add(MessageData data)
{
double sum = data.m_addend1 + data.m_addend2;
return sum;
}
#endregion
}
}