PHP and Web Services study notes

zhaozj2021-02-16  85

source:

http://www.douzi.org/blog

Learning materials are: [WROX] Professional Open Source Web Services

Chapter 8 PHP and Web Services

English e-book download [PDF]

The full class is divided into three parts:

Overview. PHP performs the advantages of Web Services development, installing PHP on the UNIX system Using SOAP in PHP. NusoAP Toolkit, NusoAP's Advanced Web Service features such as HTTP Agents, SOAP over Https, Document Style Messaging. Also discuss how some PHP Web Services programming will encounter, such as security issues, language to data types of XML-RPC in PHP. The characteristics of XML-RPC, the comparison of XML-RPC and SOAP, and then use Useful, Inc. implementation to create XML-RPC clients and server programs below are the first part.

Section 1. Overview PHP has been built into XML support by binding the Expat Parser, additional extensions, such as DOMXML (providing DOM, XPath, XLink support by using the libXML library), XSLT (for complex A third-party XSLT library such as an outsourcing program provided by Sablotron and LibxSLT.

Another PHP extension for Web Service development is CURL (Client Url Library). CURL allows you to communicate through different protocols, such as HTTP, HTTPS, FTP, Telnet, LDAP, where HTTPS is especially useful for Web Services and servers.

SOAP VS XML-RPC advantages and disadvantages:

Powerful Type Extensions (SOAP) User Custom Character Set, such as US-ASCII, UTF-8, UTF-16 (SOAP) Specifies Recipient [Specify Container?] (SOAP) Container failed (SOAP) ) Easy to use (XML-RPC) design is simple (XML-RPC)

Configure PHP:

Apache: In order to run PHP as an Apache module, use the --with-apxs option, such as --with-apxs = / www / bin / apxs. [I use the apache2, I compile the option to use the option --with-apxs2 = / usr / sbin / apxs] Domxml: optional feature, which is very helpful to parsing the XML document. Need to pre-install the libXML library (version> = 2.4.2), use -With-Dom = DIR option (default DIR /usR )http://www.xmlsoft.org/downloads.htmllibxml 2.6.4 - Sources - 2.52 MB XSLT: Optional feature, helps the convert XML data to other types of documents help. Use the --enable-xslt --with-xslt-sablot option when compiling. The Sablotron XSLT library (http://www.gingerall.com/) must be installed in advance, (default DIR is / usr / lib or / usr / local / lib). Sablotron 1.0.1 - Sources - 470 KB CURL: As mentioned earlier, if the SSL support is provided, it must be installed. Use the --with-curl = dir option when compile. It also needs to be pre-installed in advance, version> = 7.0.2-Beta). My PHP has been installed. CURL Information: libcurl / 7.10.7 openssl / 0.9.7c zlib / 1.1.4]

Continue yesterday's study. Today is mainly about NusoAP

Section 2. SOAPNUSOAP Introduction: NusoAP is a set of open source, used to send and receive SOAP messages via HTTP, by Nusphere Corporation

Http://www.nusphere.com

Development. One advantage of NusoAP is that he is not an extension, but written purely with PHP code, so the scope of application is relatively wide.

structure:

Installation configuration: from

http://dietrich.ganx4.com/nusoAP/ Download, release the Nusoap.php file from the zip file to the include directory, before your script

INCLUDE ('Nusoap.php');

Just get it.

Example: Below is a simple SOAP Client program: soap_client.php

carried out

Call ('echostring', $ parameters); // Error detection if (! $ Err = $ S-> getError ()) {echo 'result:'. $ results {echo 'error:'. $ ERR;} // debug, the following is the SOAP request (response) Packets, including HTTP head echo "

". $ S-> Request. "</ Xmp>"; echo "<xmp>". $ S-> Response. "</ Xmp>";?>></p> <p>Corresponding Server-Diffuse: SOAP_SERVER.PHP</p> <p><? php // Simple ServerRequire ('Nusoap.php'); // Create a new SOAP_SERVER object, and register how to allow remote calls $ s = new soap_server; $ s-> register ('echostring'); $ s -> Register ('echoarray'); / * [article says: Miss registration, any PHP function will be able to call remote calls, which will be a great security hazard. But I tried that registration is necessary. And only the function of RETURN can be directly declared as a remote method, such as echo () is not, but strtolower () can. ] * / function echostring ($ INPUTSTRING) {// Cyries IF (is_String ($ INPUTSTRING)) {Return "Hello,". $ INPUTSTRING;} else {// SOAP_FAULT class is used to generate error message return new soap_fault (' Client ',' ',' The Parameter To this Service Must Be a string. '); // soap_fault (faultcode, faultactor, faultstring, faultdetail); // is the format of the error handling class for the buffer // Faultcode must . Can be set to client or server to indicate which end occurs on. // faultactor has not been implemented in NusoAP. // faultString error message. // faultdetail detailed error message. You can use an XML tag. In addition to the constructor, the SOAP_FAULT class has a serialize () method // which sequences the error message, then returns a complete SOAP packet, sample: / * $ fault = new soap_fault ('client', '' , 'The InputString Parameter Must Not Be Empty'); Echo $ FAULT-> Serialize (); * /}} // Demo the array type using Function Echoarray ($ INPUTSTRING) {Return $ InputString [0]. " ". $ INPUTSTRING [1];} // The last step is to pass all received POST data to the SERVICE method for SOAP Server. It will process the request and call the corresponding function. $ S-> Service ($ http_raw_post_data);?> Use of complex data types:</p> <p>Array. The following is a Body section of the generated SOAP:</p> <p><SOAP-ENV: BODY> <ns1: echoarray xmlns: ns1 = "http://testuri.org"> <soapval xsi: type = "soap-enc: array" soap-enc: arraytype = "xsd: string [2 ]> <Item xsi: type = "xsd: String"> string1 </ item> <item xsi: type = "xsd: string"> string2 </ item> </ soapval> </ ns1: echoarray> </ soap -ENV: Body> Generates Composite Types Samples, uses SOAPVAL. The following is a Body section of the generated SOAP:</p> <p><myaddress xmlns: ns3398 = "http://mynamespace.com" xsi: type = "ns3398: address"> <street xsi: type = "xsd: string"> 123 freezing lane </ street> <city xsi: type = "xsd: string"> Nome </ city> <state xsi: type = "xsd: string"> alaska </ state> <zip xsi: type = "xsd: int"> 12345 </ zip> <phonenumbers> <home XSI: Type = "xsd: string"> 1234567890 </ home> <mobile xSI: type = "xsd: string"> 0987654321 </ mobile> </ phonenumbers> </ myaddress></p> <p>Procedure example: soapval.php execution</p> <p><? php // SOAPVAL: General Compound Types SamplesInClude ('Nusoap.php'); $ address = array ('street' => '123 freezing lane', 'city' => 'Nome', 'State' => ' Alaska ',' Zip '=> 12345,' phonenumbers '=> array (' home '=>' 1234567890 ',' Mobile '=>' 0987654321 ')); $ s = new soapval (' myaddress ',' address " $ address, '', 'http://mynamespace.com'); Print "<XMP>". $ s-> serialize (). "</ xmp>";?>></p> <p>WSDL</p> <p>WSDL is an XML language for describing the Web Service. It is an opportunity format that provides information necessary for all access services to the Web Service client. NusoAP provides a class for parsing of WDSL files and extracts information. SOAPCLIENT objects use the WSDL class to mitigate the difficulty of developers calling services. Create a message through the help of WSDL information, programmers only need to know the names and parameters of the operation to call it. Provide the following advantages to using WSDL with WSDL through NusoAP:</p> <p>All service meta files, such as namespaces, EndPoint URLs, parameter names, and the like, which allows client-side-end-to-end changes. Since the server can be obtained at any time, these data no longer need to use hard coding in the user script. It allows us to use the SOAP_PROXY class. This class is derived from the SOAPCLIENT class, increasing the method corresponding to the operation described in detail in the WDSL file. Now the user can call these methods by it. The SOAPCLIENT class contains a getProxy () method that returns an object of a SOAP_PROXY class. The SOAP_PROXY class is derived from the SOAPClient class, which adds a method corresponding to the operation defined in the WSDL document, and allows the user to call an endpoint remote method. This is only applicable to the case where the SOAPCLIENT object is initialized with WDSL files. The advantage is that it is easy to use, the disadvantage is that the performance -PHP creates an object is time-consuming - and does not serve the utilitarian service (And this functionality service no utilitarian purpose).</p> <p>Example: WSDL.PHP</p> <p>carried out</p> <p><? PHP // WSDL A simple demo file include ('Nusoap.php'); // SOAP source is a service / / service // first we created a SOAPCLIENT object, pass the URL of the WSDL file to The constructor is used to use the second parameter to make the client know that we pass the WSDL instead of SOAP Endpoint. $ s = new soapclient ('http://www.abundanttech.com/webservices/deadoralive/deadoralive.wsdl', 'wsdl'); // Generate Proxy class $ P = $ S-> getProxy (); // call Remote function $ SQ = $ P-> gettoDaysbirdhdays (); if (! $ ERR = $ P-> getError ()) {Print_R ($ SQ);} else {print "error: $ Err";} print 'request: <XMP> '. $ P-> Request.' </ xmp> '; Print' response: <xmp> '. STR_REPLACE ('> <', "> / n <", $ p-> response).' < / XMP> ';?></p> <p>Attached: A demo site for useful web services:</p> <p>http://www.mindreef.net/soapscope/wsdldemo</p> <p>This is the last section, I feel that PHP is still very suitable for the Client. This section is mainly about XML-RPC. I used to have an XML-RPC learning note, and the content is very similar.</p> <p>Section 3. XML-RPC</p> <p>XML-RPC data type</p> <p>XML-RPC only supports limited number of data types. Below is the corresponding relationship with the PHP data type:</p> <p>Useful Inc. XML-RPC Implementation - Phpxmlrpc We use the XML-RPC kit for useful, Inc. EDD Dumbill, download URL http://phpxmlrpc.sourceforge.net, where the complete include Client and Server XML- RPC implementation. The client and server end are implemented by the XMLRPC_Client class and XMLRPC_Server class, primarily for receiving and transmitting XML-RPC messages. XMLRPCVAL class is used to encode PHP variables to the XML-RPC equivalent data type and pass parameters to the remote method. The opposite process uses the XMLRPC_DECode () function. XML-RPC messages are created using the XMLRPCMSG class by passing to one of the parameter tables. The XMLRPC_Client class sends an XML-RPC packet created using the XMLRPCMSG class. On the server side, the XMLRPC_Server class parses these received packets as an XMLRPCMSG object. This object is then passed to the user function as a separate parameter. This function must return an XMLRPCRESP object, and the XMLRPC_Server class uses it for serialization and returns to the client. This basic architecture is shown below.</p> <p>Installation and configuration</p> <p>Download in http://phpxmlrpc.sourceforge.net, unpack, and then place XMLRPC.INC and XMLRPCS.Ic to your included path. The client program headed only needs to add the following containing statement: include ('xmlrpc.inc'); server-side program header should join the following include statement: include ('xmlrpc.inc'); include ('xmlrpcs.inc'); / / Server-side code</p> <p>example</p> <p>XML-RPC Client: XMLRPC_CLIENT.PHP Execution</p> <p><? php // xmlrpc_client.php // XML-RPC client demo request ('xmlrpc.inc'); // Create a client object, three parameters are Path, Hostname, Port $ s = new xmlrpc_client ('/ XMLRPC / XMLRPC_SERVER.PHP ',' www.windix.local ', 80); // CREATE XMLRPCVAL OBJECT, Which Allows the Encoding Of Our Variable // Create XMLRPCVAL object, encode our PHP variable as XML required for XML-RPC Form $ INPUTSTRING = New Xmlrpcval ('World'); // Create An Array Of Parameters // Although we only have one parameter, but still want to convert into an array, because XMLRPCMSG's second parameter is a parameter Table $ parameters = array ($ INPUTSTRING); // CREATE The message Object // Create XML-RPC packets, parameters are remote method names and parameter table $ msg = new xmlrpcmsg ('echostring', $ parameters); // send The message, get the response // Send a message, return value $ rsp is an XMLRPCRESP object, which contains the following three methods: // faultcode () error code, if successful will return 0 // FaultString () error message //// Value () return value, exists in the form of Xmlrpcval objects, PHP needs to decode $ r = $ s-> send ($ msg); // check for errorsif ($ rp-> faultcode () == 0) {/ / decode the response to a php type // xmlrpc_decode () function is used to decode the XMLRPCVAL object $ response = Xmlrpc_Decode ($ RSP-> Value ()); // Print Results Print '<pre>'; var_dump ($ response) ; Print '</ pre>';} else {//prin TERRORS Print 'error:'. $ rp-> faultcode (). ','. $ rp-> faultstring (). '<br>';} // show message // then let's check the message content $ MSG-> CreatePayLoad (); Print 'Request: <xmp>'. $ msg-> payload. '</ xmp>'; print 'response: <xmp>'. $ rP-> serialize (). '</ xmp > ';?> XML-RPC Server: XMLRPC_SERVER.PHP</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-12853.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="12853" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.037</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'EexoI3dO9WEVxDpHeW07NPM6_2FVEd0flfU32f64_2F9ss8PUiH5zmzU2BHgQotaCPTc4YCiaehJccghS7Ra9fI4EA_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>