May 6, 2010 by Christoff Truter PHP
Web services are useful for a number of things, e.g. we can provide companies with data without needing
to give them direct access to our databases.
One of the most important uses (in my opinion) is the interoperability it provides, which allows different
platforms, devices etc to interact with each other.
In this post we're going to expose a web service in PHP using NuSphere's Soap library,
which has been in development since 2002 - which like the name suggests relies on the SOAP protocol (XML based) and consume it in a C# application using WCF.
First of all you will need to include the library and instantiate a new soap_server object, like this:
include "lib/nusoap.php"; $namespace = "http://www.cstruter.com"; $server = new soap_server(); $server->soap_defencoding = 'UTF-8'; $server->configureWSDL("TestService", $namespace); $server->wsdl->schemaTargetNamespace = $namespace;
$server->register('test', // function name array("name"=>"xsd:string"), // parameters array('return'=>'xsd:string'), // return value type $namespace); function test($name) { return "Hello $name"; }
$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : @file_get_contents("php://input"); $server->service($POST_DATA);
using System; using cstruter.com.Service; // Namespace defined in the Add Service Reference dialog namespace cstruter.com { class Program { static void Main(string[] args) { TestServicePortTypeClient TestService = new TestServicePortTypeClient(); String value = TestService.test("Jurgens"); Console.WriteLine(value); // Outputs Hello Jurgens } } }
$server->wsdl->addComplexType( 'post', // Name of the object 'complexType', // Object Type (Why? Since we've got an addSimpleType method?) 'struct', // Struct /Array (Multiple objects) 'all', // Composition '', // Restriction Namespace array( 'postID' => array('name' => 'postID', 'type' => 'xsd:int'), 'Title' => array('name' => 'Title', 'type' => 'xsd:string'), 'Body' => array('Body' => 'Body', 'type' => 'xsd:string') ) // Structure Defintion );
$server->register('test2', array("name"=>"tns:post"), array('return'=>'xsd:string'), $namespace); function test2($value) { return print_r($value, true); }
TestServicePortTypeClient TestService = new TestServicePortTypeClient(); post p = new post { postID = 1, Title = "Test 1 2 3", Body = "Some test" }; String value = TestService.test2(p); Console.WriteLine(value);
$server->wsdl->addComplexType( 'posts', // Name of the object 'complexType', 'array', // Array since we're passing a set of objects '', 'SOAP-ENC:Array', array(), array( array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:post[]') ), 'tns:post' // Child object );
$server->register('test3', array(), // Blank array if we don't have any parameters. array('return'=>'tns:posts'), // The array of objects. $namespace); function test3() { return array(array('postID'=>1, 'Title'=>'test 1', 'Body'=>'abc abc abc'), array('postID'=>2, 'Title'=>'test 2', 'Body'=>'123 123 123') ); }
TestServicePortTypeClient TestService = new TestServicePortTypeClient(); post[] ps = TestService.test3(); foreach (post p in ps) { Console.WriteLine(String.Concat(p.postID, ":", p.Title, ":", p.Body)); }
cannot access the post object from the webservice September 14, 2013 by Gian
i cannot seem to access the object name post. I import the wsdl webservice. and i can access the simple function that sends a string and returns a string. Am using Visual Studio 2012.Great post.