WebService-php-2 (17), webservice-php-17_PHP tutorial

Source: Internet
Author: User
WebService-php-2 (17), webservice-php-17. WebService-php-2 (17), webservice-php-17wsdl instance? Xmlversion1.0encodingUTF-8? DefinitionstargetNamespacelocalhost00xmlns: tnslo WebService-php-2 (17), webservice-php-17

Wsdl instance

 
 
  
  
   
  
  
  
   
  
  
   
  
  
  
   
    
    
   
  
  
  
   
   
   
   
    
    
    
    
     
   
  
  
  
   
    
   
  
 

Server example:

function test($x) {return $x;}$ss = new SoapServer('http://localhost/00/wsdl.xml');$ss->addFunction('test');$ss->handle();

Client call:

$soap = new soapClient('http://localhost/00/wsdl.xml',array('trace'=>true));var_dump($soap->test('10086'));

Passing and returning array parameters
If the transmitted or returned parameters are arrays, you can describe them in the message label.

 
  
 
 
  
 

XML-RPC call

XML-RPC can be understood as a simplified version of soap, the packaging of data is relatively simple. php. ini, to open extension = php_xmlrpc.dll
/* When calling a function, the rpc server transmits the following parameters: array (0 => 'function name', 1 => array (real parameter 1, real parameter 2 ,... real parameter N), 2 => NULL) */function hello () {return 'hello';} function sum ($ method, $ args, $ extra) {return array_sum ($ args);} // Create an RPC Server $ server = xmlrpc_server_create (); xmlrpc_server_register_method ($ server, 'hello', 'Hello'); xmlrpc_server_register_method, 'sum', 'sum'); // receives a request $ request = $ HTTP_RAW_POST_DATA; // retrieves the execution result after executing the XML request to call the client $ xmlrpc_response = xmlrpc_server_call_method ($ server, $ request, null); // output the result XML after function processing ('content-Type: text/XML'); echo $ xmlrpc_response; // destroy the XML-RPC server-side resource xmlrpc_server_destroy ($ server );

Client:

class rpcclient {protected $url;public function __construct($url='' ) {$this->url = $url;}protected function 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);return xmlrpc_decode($xml);}public function __call($method , $args) {$request = xmlrpc_encode_request($method , $args);return $this->query($request);}}$rpc = new rpcclient('http://localhost/00/rpcs.php');var_dump($rpc->hello());var_dump($rpc->sum(4,5,6));

Differences between WebService and json APIs

WebService json API
Data encapsulation XML json
High Complexity
The underlying protocol is not limited to HTTP
Data types can be strictly defined and cannot be strictly defined.
Additional API documentation is required for self-explanatory self-instructions

Http://www.bkjia.com/PHPjc/993278.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/993278.htmlTechArticleWebService-php-2 (17), webservice-php-17 wsdl instance? Xml version = '000000' encoding = 'UTF-8 '? DefinitionstargetNamespace = 'http: // localhost/00/'xmlns: tns = 'http: // lo...

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.