Webservice-php-2 (17)

Source: Internet
Author: User

WSDL instance

<?xml Version ='1.0'encoding ='UTF-8'?><Definitionstargetnamespace='http://localhost/00/'Xmlns:tns='http://localhost/00/'Xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'xmlns:xsd='Http://www.w3.org/2001/XMLSchema'Xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'xmlns='http://schemas.xmlsoap.org/wsdl/'><!--<types> elements define the data types used by the Web service, WSDL uses XML Schema syntax to define the data type, or you can customize the types that the schema does not contain--><types> <xsd:schema xmlns:xsd="Http://www.w3.org/2001/XMLSchema"targetnamespace="http://localhost/00/"></xsd:schema></types><!--<message>element to define the part of each message, and the associated data type.--><message name='testrequest'><part name=" Term"Type="xsd:string"/></message><message name='Testresponse'><part name="value"Type="xsd:string"/></message><!--<portType>The element is the most important WSDL element. It describes a Web service, the operations that can be performed, and the associated messages. It tells you which WebService connection point to play and plays a controller.--><porttype name='oplist'><operation name='Test'><input message='tns:testrequest'/><output message='Tns:testresponse'/></operation></porttype><!--<binding> elements Define message format and protocol details for each port--><binding name='Cartsoap'Type='tns:oplist'><!--Style: attribute desirable value"RPC"Or"Document"A Ransport: property defines the SOAP protocol to use. In this example we use http--><soap:binding style='RPC'Transport='http://schemas.xmlsoap.org/soap/http'/><!--The operation element defines the operators provided for each port, and for each operation, the corresponding SOAP behavior needs to be defined--><operation name='Test'><soap:operation soapaction='http://www.cwtservice.cn/newOperation/'/><input><soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'Encodingstyle='http://schemas.xmlsoap.org/soap/encoding/'/></input><output><soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'Encodingstyle='http://schemas.xmlsoap.org/soap/encoding/'/></output></operation></binding><!--<service> contains one or more port elements, Each port element represents a different Web service--><service name='SHOPWS'><port name='Cartsoap'binding='Tns:cartsoap'><soap:address location='http://localhost/00/wss.php'/></port></service></definitions>

Server-side Example:

function Test ($x) {returnnew soapserver ('http://localhost/00/ Wsdl.xml'); $ss->addfunction ('test'); $ss Handle ();

Client invocation:

new soapclient ('http://localhost/00/wsdl.xml', Array ('Trace  '=(True)); Var_dump ($soap->test ('10086'));

Passing and returning array parameters
If the parameter passed or returned is an array, it can be described in the message label.

<message name='testrequest'><part name="term" type ="xsd:arrayofstring"/></message><message name='  Testresponse'><part name="value" type="  Xsd:arrayofstring"/></message>

XML-RPC Call

xml-RPC can be understood as a simplified version of SOAP, and the packaging of the data is relatively concise. PHP.ini, to open extension=php_xmlrpc.dll
/*Summing function Note that when the RPC server invokes a function, the arguments passed are: Array (0=> ' function name ', 1=>array (argument 1, argument 2,... Argument n), 2=>null)*/function Hello () {return 'Hello';} function sum ($method, $args, $extra) {returnarray_sum ($args);}//creating the RPC Server$server =xmlrpc_server_create (); Xmlrpc_server_register_method ($server,'Hello','Hello'); Xmlrpc_server_register_method ($server,'sum','sum');//Collect requests$request =$HTTP _raw_post_data;//get execution results after calling the client's XML request$xmlrpc _response = Xmlrpc_server_call_method ($server, $request,NULL);//output The resulting XML after the function is processedHeader'Content-type:text/xml'); Echo $xmlrpc _response;//destroying XML-RPC server-side ResourcesXmlrpc_server_destroy ($server);

Client:

classrpcclient {protected$url; Publicfunction __construct ($url ="' ) {$ This->url =$url;}protectedfunction Query ($request) {$context= Stream_context_create (Array ('http'=Array ('Method'="POST",'Header'="Content-type:text/xml",'content'=$request))); $xml= File_get_contents ($ This->url,false, $context);returnXmlrpc_decode ($xml);} Publicfunction __call ($method, $args) {$request=xmlrpc_encode_request ($method, $args);return$ This-query ($request);}} $rpc=NewRpcclient ('http://localhost/00/rpcs.php'); Var_dump ($rpc-hello ()); Var_dump ($rpc->sum (4,5,6));

The difference between WebService and the JSON API

WebService JSON API
Data Encapsulation XML JSON
Complexity of the high and low
Underlying protocol unlimited HTTP
Data types can be strictly defined and not strictly defined
Self-explanatory self-explanatory additional API documentation required

Webservice-php-2 (17)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.