Monday, 15 March 2010

how to call a method using soap in php -



how to call a method using soap in php -

i have created php file. contains details access (wsdl)xml file , function. need phone call function paramaters can't able phone call function. in function need print value of parameters.$ve = $soap_client->sayhello($b);--->>>while executing line getting error couldnot connect host(it straight passed catch).anyone plz point me error did in code. passed value in url "http://xxx.xxx.x.xx/testcode.php?method=sayhello&name=shankar" in php file testcode.php:

<?php seek { $soap_client=new soapclient("helloworld.wsdl"); $a = $_get["method"]; $b = $_get["name"]; echo $b; $ve = $soap_client->sayhello($b); function sayhello($b) { echo 'continue'; echo $b; } } catch(soapfault $exception) { echo $exception->getmessage(); } ?>

try set location param if not yet exist in wsdl schema:

wsdl mode:

$client = new soapclient('helloworld.wsdl', array( 'location' => 'http://xxx.xxx.x.xx/testcode.php', ));

non wsdl mode:

$client = new soapclient(null, array( 'location' => "http://xxx.xxx.x.xx/testcode.php", 'uri' => "urn://etc", 'trace' => 1 )); echo $client->sayhello('foo');

server illustration non wsdl mode:

class service { public function sayhello($name) { homecoming 'hello ' . $name; } } $server = new soapserver(null, array('uri' => "urn://etc/")); $server->setclass("service"); $server->handle();

and similar wsdl..

also can utilize php-wsdl-creator generated new wsdl data

added:

full sample wsdl mode - step step:

1) create simple illustration service:

class="lang-php prettyprint-override">class service { /** * @param string $name * @return string */ public function sayhello($name) { homecoming 'hello ' . $name; } } $server = new soapserver('http://example.com/wsdl-gen/'); //our auto-gen. schema $server->setclass("service"); $server->handle();

2) new generated schema, utilize php-wsdl-creator (go next step if have)

class="lang-php prettyprint-override">require_once 'php-wsdl/class.phpwsdl.php'; $namespace = 'http://example.com/'; $location = 'http://example.com/server.php'; $wsdl = phpwsdl::createinstance($namespace, $location, './cache', array( '../soap-server.php' // classes generator ), null, null, null, false, false); //$wsdl->runserver(); // bonus :) generated total client documentation $wsdl->optimize = false; $wsdl = $wsdl->createwsdl();

3) wsdl-schema can be:

<wsdl:definitions xmlns:tns="http://example.com/" targetnamespace="http://example.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/xmlschema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"> <wsdl:message name="sayhellosoapin"> <wsdl:part name="name" type="s:string" /> </wsdl:message> <wsdl:message name="sayhellosoapout"> <wsdl:part name="return" type="s:string" /> </wsdl:message> <wsdl:porttype name="servicesoap"> <wsdl:operation name="sayhello"> <wsdl:input message="tns:sayhellosoapin" /> <wsdl:output message="tns:sayhellosoapout" /> </wsdl:operation> </wsdl:porttype> <wsdl:binding name="servicesoap" type="tns:servicesoap"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc" /> <wsdl:operation name="sayhello"> <soap:operation soapaction="http://example.com/sayhello" /> <wsdl:input> <soap:body use="encoded" encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://example.com/" parts="name" /> </wsdl:input> <wsdl:output> <soap:body use="encoded" encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://example.com/" parts="return" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="service"> <wsdl:port name="servicesoap" binding="tns:servicesoap"> <soap:address location="http://example.com/soap-server.php" /> </wsdl:port> </wsdl:service> </wsdl:definitions>

4) create illustration client current scheme:

$client = new soapclient( 'http://example.com/wsdl-gen/', //schema array( 'location' => 'http://test/server.php' //soap server addr //we provide alternative if location param not valid in scheme or not exist ));

5) that's all, illustration working our service methods:

$client->sayhello('reg'); //string 'hello reg' (length=9)

documentation , instruments:

phpwsdl - wsdl generator php. (i used in job ~2 year ago) documentation web services - soap in php soapclient class soapserver class

php soap wsdl

No comments:

Post a Comment