Use of XML-RPC functions in PHP5

xiaoxiao2021-03-06  88

In the past few days, I saw the winning work of Zend PHP5 Code Contest, and I saw a winning work on the web architecture, and I feel deeply, XML and PHP combined with more and more, almost all works in design. I used XML this thing .....-_-! It's a lot of it. It is recommended that friends go see. So I am in a few days of learning PHP and XML combination related knowledge. This is encountered In the XML-RPC service, there are not many materials available on the Internet, and most of them use other third-party XML-RPC Class developed, and the XMLRPC function comes with PHP5 (currently the test module) There are not many, but individuals still like native things, I have studied themselves.

In the manual, someone provided an example, but did not support Chinese, and did not tell this result after the client returned the result. I have been working hard for half a day, but I can easily let him support Chinese and almost no need. Do any additional work.

Below is his original example (can be found in the manual): / * ClientTest.php * /

IF (! FPUTS ($ FP, $ Query, Strlen)) {$ 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, EG PRINT IT * /?>>

/ * Servertest.php * /

$ server = xmlrpc_server_create ();

/ * Register the 'external' name and then the 'internal' name * / xmlrpc_server_register_method ($ server, "cycle", "lifecycle"); $ request = $ HTTP_RAW_POST_DATA; // no you do not need 'always on', And no $ _POST DOESN'T WORK.

/ * The Parameters Here Are 'Server, XML-String and User Data'. There's Suppodosed To Be An Optional 'Output Options' Array Too, But I Can't Get It Working :( Hence header () call * / $ response = XMLRPC_Server_Call_method ($ Server, $ Request, Null); Header ('Content-Type: Text / XML'); Print $ Response;

XMLRPC_SERVER_DESTROY ($ server);?>

In this example, the client does not have any processing after $ response, this return value is the string of this look: http / 1.1 200 okdate: THU, 04 Nov 2004 08:21:43 Gmtserver: apache / 2.0.47 (Win32 PHP / 5.0.1X-POWERED-by: PHP / 5.0.1Connection: CloseContent-Type: Text / XML; Charset = GB2312

all eggs will be birds one day.

This string contains two parts, one is the header information of HTML, one is an XML-RPC package file, which can meet our requirements for him.

Let's take a look at the example I have modified, mainly used an XMLRPC_Decode function, and I didn't find a description online. I tried it for a long time. I tried his usage: / * ClientTest.php * /

$ 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 / n ". $ request." / n ";

IF (! FPUTS ($ FP, $ Query, Strlen)) {$ Errstr = "Write Error"; Return 0;}

$ contents = ''; while (! Feof) {$ 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, EG Print it * /

$ POS = STRPOS ($ response, '

/ * Do Something with $ TMP E.G. Print it * /?>

? / * Servertest.php * /

$ 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 Suppodosed To Be An Optional 'Output Options' Array Too, But I Can't Get It Working :( Hence header () call * / $ response = XMLRPC_Server_Call_Method ($ Server, $ Request, Null); Header ('Content-Type: Text / XML'); Print ($ Response);

XMLRPC_SERVER_DESTROY ($ server);?>

Run, look at the style of the array display, so that it is more comfortable.

Understand this, don't be idle, look at the "SOAP Functions", how to use the PHP5 comes, and the same is small.

/ ************************** * Author: old Youth * email: wnadmin@sina.com * from: http: // blog. 9cbs.net/hahawen ********************************** /

转载请注明原文地址:https://www.9cbs.com/read-96308.html

New Post(0)