The php5|xml| function looks at several award-winning works on the Web architecture, feels pretty deep, XML and PHP are used more and more, and almost all of the works in the design use XML this thing ...-_-! And a lot of behind. Friends are advised to have a look. So myself these days in crazy learning PHP and XML combined with the relevant knowledge.
This encountered the XML-RPC services, there is little information available on the Internet, and most of them are using XML-RPC class developed by other third parties in PHP, and the php5 xmlrpc function (currently the test module) seems to be not much, But the individual or more like the original things, they have studied.
An example is provided in the manual, but it does not support Chinese and does not tell you how to handle the result after the client returns results. I finally figured out how hard it was to get him to support Chinese and hardly any extra work.
Here is his original example (which can be found in the manual):
* clienttest.php * *
<?php
function Do_call ($host, $port, $request) {
$fp = Fsockopen ($host, $port, $errno, $ERRSTR);
$query = "post/servertest.php http/1.0\nuser_agent:my Egg client\nhost:". $host. " \ncontent-type:text/xml\ncontent-length: ". strlen ($request)." \ n ". $request." \ n ";
if (!fputs ($fp, $query, strlen ($query)) {
$ERRSTR = "Write error";
return 0;
}
$contents = ';
while (!feof ($fp)) {
$contents. = Fgets ($FP);
}
Fclose ($FP);
return $contents;
}
$host = ' localhost ';
$port = 80;
$request = Xmlrpc_encode_request (' cycle ', ' egg ');
$response = Do_call ($host, $port, $request);
/* Do something with $response, e.g. print it * *
?>
* servertest.php * *
<?php
function Lifecycle ($method, $params) {
/* $method = ' cycle ', $params = (array of) request parameter (s); $data is also passed from Xmlrpc_server_call_method, if we had no.
switch ($params [0]) {
Case ' egg ':
$reply = ' All eggs is birds one day. '
Break
Default
$reply = ' That must have been a otheregg ';
}
return $reply;
}
$server = Xmlrpc_server_create ();
/* Register the ' external ' name and then ' internal ' name * *
Xmlrpc_server_register_method ($server, "cycle", "lifecycle");
$request = $HTTP _raw_post_data; No you don ' t need ' always on ', and no $_post doesn ' t work.
/* The parameters here are ' server, xml-string and user data '. There ' s supposed to be a optional ' output options ' array too, but I can ' t get it working:(hence header ()
$response = Xmlrpc_server_call_method ($server, $request, NULL);
Header (' Content-type:text/xml ');
Print $response;
Xmlrpc_server_destroy ($server);
?>
In this example, the client gets $response and does not do any processing, and the return value is a string like this:
http/1.1 OK
Date:thu, Nov 08:21:43 GMT
server:apache/2.0.47 (WIN32) php/5.0.1
x-powered-by:php/5.0.1
Connection:close
content-type:text/xml;charset=gb2312
<?xml version= "1.0" encoding= "Iso-8859-1"?>
<methodResponse>
<params>
<param>
<value>
<string>all eggs would be birds one day.</string>
</value>
</param>
</params>
</methodResponse>
This string contains two parts, one is HTML header information, one is the XML-RPC package file, a little bit of processing will be able to meet our requirements.
The following look at the example I have modified, mainly using a xmlrpc_decode function, the Internet did not find a description, I tried for a long time, just try out his usage:
* clienttest.php * *
<?php
function Do_call ($host, $port, $request) {
$fp = Fsockopen ($host, $port, $errno, $ERRSTR);
$query = "post/servertest.php http/1.0\nuser_agent:my Egg client\nhost:". $host. " \ncontent-type:text/xml\ncontent-length: ". strlen ($request)." \ n ". $request." \ n ";
if (!fputs ($fp, $query, strlen ($query)) {
$ERRSTR = "Write error";
return 0;
}
$contents = ';
while (!feof ($fp)) {
$contents. = Fgets ($FP);
}
Fclose ($FP);
return $contents;
}
$host = ' localhost ';
$port = 80;
$request = Xmlrpc_encode_request (' cycle ', ' egg ');
$response = Do_call ($host, $port, $request);
/* Do something with $response, e.g. print it * *
$pos = Strpos ($response, ' <?xml ');
$str = substr ($response, $pos);
$tmp = Xmlrpc_decode ($STR);
Print_r ($TMP);
/* do something with $tmp e.g. print it */
?>
* servertest.php * *
<?php
function Lifecycle ($method, $params) {
/* $method = ' cycle ', $params = (array of) request parameter (s); $data is also passed from Xmlrpc_server_call_method, if we had no.
switch ($params [0]) {
Case ' egg ':
$reply [] = ' all eggs'll be birds one day. "Good people in my city";
$reply [] = ' all eggs is birds one day. The good people aaaaa in our city;
Break
Default
$reply [] = ' That must have been a Otheregg good man in our city ';
}
return $reply;
}
$server = Xmlrpc_server_create ();
/* Register the ' external ' name and then ' internal ' name * *
Xmlrpc_server_register_method ($server, "cycle", "lifecycle");
$request = $HTTP _raw_post_data; No you don ' t need ' always on ', and no $_post doesn ' t work.
/* The parameters here are ' server, xml-string and user data '. There ' s supposed to be a optional ' output options ' array too, but I can ' t get it working:(hence header ()
$response = Xmlrpc_server_call_method ($server, $request, NULL);
Header (' Content-type:text/xml ');
Print ($response);
Xmlrpc_server_destroy ($server);
?>
Run it and look at the style of the displayed array, which is much more comfortable to handle.
Understand this, do not idle, look at PHP5 's own "SOAP functions", the use of methods and working principle, the same.