Using Xmlrpc in PHP

Source: Internet
Author: User

PHP is simple to use XMLRPC, the server side and the client are all PHP code implementation.

The XML-RPC used here fully includes the XML-RPC implementation of the client and server.
The client and server side are implemented by the Xmlrpc_client class and the Xmlrpc_server class, and are mainly used for receiving and sending XML-RPC messages.

Client:

The Xmlrpcval class is used to encode PHP variables as XML-RPC equivalent data types and to pass parameters to remote methods. The opposite procedure uses the Xmlrpc_decode () function.

The Xmlrpcmsg class creates a XML-RPC message by passing it a parameter table.
The Xmlrpc_client class sends XML-RPC messages created using the Xmlrpcmsg class.

Server-side:

The Xmlrpc_server class parses these received messages (Xmlrpcmsg objects) as PHP variables.

It is then passed as a separate parameter to the user function, which must return a Xmlrpcresp object.

The Xmlrpc_server class uses it for serialization and returns it to the client.

Server side: xmlrpc_s.php

<?PHP/*XMLRPC Server (PHP version)*/require"Libphpxmlrpc/xmlrpc.inc";require"Libphpxmlrpc/xmlrpcs.inc";functionSaySomething ($msg) {//decodes a parameter from a MSG object into a variable $words= Php_xmlrpc_decode ($msg->getparam (0)); //return Results if(strlen($words) > 0)  {     return NewXMLRPCRESP (NewXmlrpcval (' Server say: '.$words, ' string '));//returned to the customer } Else {       return NewXmlrpcresp (0,$xmlrpcerruser+, "paras".$words. "' Understand."); //error returned to the customer, of course, you can not return//if there is any wrong parameter AH system will return error//$xmlrpcerruser + 100 This is the error code to return customer custom errors// When developing the RPC program, it is best to define an error table. The error number displayed by the client is }}//set up a server$s=NewXmlrpc_server (Array(' say ' =Array(' function ' = ' saysomething ',//command corresponds to the function to be called' Signature ' =Array(Array(' String ', ' string '),//returns the input data type, a function can have several input and output types' DocString ' = ' This service echoes Hello+input stirng. '),//description of the call0//This parameter determines that the service is not immediately opened at this time );//Setting Parameters$s->response_charset_encoding = "UTF-8"; //Open Service $s-service ();?>

Client: xmlrpc_c.php

<?PHP//introduction of the XMLRPC Development Libraryrequire(' Libphpxmlrpc/xmlrpc.inc ');//establishing a connector$client=NewXmlrpc_client ('/phpworkspace/s.php ', ' localhost ', 80);$client->request_charset_encoding = ' UTF-8 ';//you can set up debugging to view detailed HTTP request Informationif($_get[' Debug '])$client->setdebug (true); //create Xmlrpcval object, encode PHP variable as XML-RPC required XML form$input=NewXmlrpcval (' hello! ', ' string ');//parameters are placed in the array$paras=Array($input);//Creating XML-RPC Messages$msg=NewXmlrpcmsg (' Say ',$paras);//send the request with the connector you just created$RSP=$client->send ($msg);//processing the return value of the serverif(0 = =$RSP-FaultCode ()) {    //decoding    $response= Php_xmlrpc_decode ($RSP-value ()); //Show Results    #Var_dump ($response);    Echo $response;} Else{    //error occurred    Print' Error: '.$RSP->faultcode (). ', '.$RSP->faultstring (). ";}?>

  
Opens the client file in the browser, such as:


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.