Php Webservice communication

Source: Internet
Author: User
WebService is a new web application branch. it is a self-contained, self-describing, and modular application that can be released, located, and called through the web. WebService can execute any function from simple requests to complex business processing. Web services are generated for the communication of heterogeneous systems, its basic idea is to use XML-based HTTP remote call to provide a standard mechanism, saving the need to establish a new protocol. At present, there are two protocol standards for Web Service communication, one is XML-RPC, the other is SOAP. XML-RPC is relatively simple, the appearance of time is relatively early, SOAP is more complex, mainly in some need of stability, robustness, security and complex interaction when using.

PHP integrates access to both XML-RPC and SOAP protocols, both of which are concentrated in xmlrpc extensions. In addition, in php pear, whether PHP 4 or PHP 5, has been integrated with the XML-RPC extension by default, and the extension has nothing to do with xmlrpc extension, can independently implement XML-RPC protocol interaction, if there is no xmlrpc extension, we recommend using the PEAR: XML-RPC extension.

All that is said is virtual. the following example shows everything.

First, construct the webservice server:

On the server side, the function I defined is get ("helloworld"); // hello is the input parameter.

/**
* Function: the function provided to the RPC client.
* Parameters:
* $ Function to be called by the method client
* $ Params parameter array of the function to be called by the client
* Return: return the specified call result.
*/
Function rpc_server_func ($ method, $ params ){
$ Parameter = $ params [0]; // name of the input function

$ Parameter1 = $ params [1]; // input parameters
If ($ parameter = "get ")
{
$ Return = "This data by get method". $ parameter1;
}
Else
{
$ Return = "Not specify method or params ";
}
Return $ return;
}

// Generate a XML-RPC on the server side
$ Xmlrpc_server = xmlrpc_server_create ();

// Register a method called by the server, rpc_server, which actually points to the rpc_server_func function
Xmlrpc_server_register_method ($ xmlrpc_server, "rpc_server", "rpc_server_func ");

// Accept XML data POST from the client
$ Request = $ HTTP_RAW_POST_DATA;

// Execute the XML request to call the client and obtain the execution result
$ Xmlrpc_response = xmlrpc_server_call_method ($ xmlrpc_server, $ request, null );

// Output the result XML after function processing
Header ("Content-Type: text/xml ");
Echo $ xmlrpc_response;

// Destroy XML-RPC server resources
Xmlrpc_server_destroy ($ xmlrpc_server );
?>



Use php to access the server of the defined webservice.

/**
* Function: provides a function to the client to connect to the XML-RPC server.
* Parameters:
* $ Host the host to be connected
* $ Port: port used to connect to the host
* $ Rpc_server XML-RPC server files
* $ XML request information encapsulated by request
* Return: If the connection succeeds, XML information returned by the server is returned. if the connection fails, false is returned.
*/
Function write_file ($ string)
{
$ Fp = fopen ("xml. log", "w ");
Fprintf ($ fp, "% s \ n", $ string );
Fclose ($ fp );
}
Function rpc_client_call ($ host, $ port, $ rpc_server, $ request ){

// Open the specified server
$ Fp = fsockopen ($ host, $ port );
// Echo $ fp ."
";
// Construct the query POST request information on the XML-RPC server that requires communication
$ Query = "POST $ rpc_server HTTP/1.1 \ r \ nUser_Agent: XML-RPC Client \ r \ nHost :". $ host. "\ r \ nContent-Type: text/xml \ r \ nContent-Length :". strlen ($ request ). "\ r \ n ". $ request. "\ r \ n ";
// Echo $ query ."
";

// Send the constructed HTTP protocol to the server. if the HTTP protocol fails, false is returned.
If (! Fputs ($ fp, $ query, strlen ($ query )))
{
$ Errstr = "Write error ";
Echo $ errstr ."
";
Return false;
}

// Obtain all the information returned from the server, including the HTTP header and XML Information
$ Contents = "";
While (! Feof ($ fp ))
{
$ Contents. = fgets ($ fp );
}

// After the connection resource is closed, the retrieved content is returned.
Fclose ($ fp );
Return $ contents;
}

// Construct the information of the RPC server.
$ Host = "localhost ";
$ Port = 80;
$ Rpc_server = "/phpserv. php"; // be sure to have/Here. do not.

// Encode the XML request to be sent into XML. the method to be called is rpc_server and the parameter is get.
$ Request = xmlrpc_encode_request ("rpc_server", array ("get", "helloworld "));
// Echo $ request;
// Call the rpc_client_call function to send all requests to the XML-RPC server for information
$ Response = rpc_client_call ($ host, $ port, $ rpc_server, $ request );
Write_file ($ response );
// Echo "aaa". $ response. "xxx "."
";
// Analyze the XML returned from the server, remove the HTTP header information, and convert the XML into a string that PHP can recognize
$ Split =" ";
// Echo "split =". $ split ."
";
$ Xml = explode ($ split, $ response );
$ Xml = $ split. array_pop ($ xml );
$ Response = xmlrpc_decode ($ xml );

/* Output the information obtained from the RPC server */
Print_r ($ response );

?>

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.