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
php // simple clientRequire ('Nusoap.php'); // To send variable $ mystring = "world"; // parameters must be passed as an array // variable must be converted into an array form $ parameters = Array ($ mystring); // Create a SOAPCLIENT object, the parameter is Server's URL $ S = New SOAPCLIENT ('http://www.douzi.org/Me/php_ws/soap_server.php'); // Call the remote method Return value stored in $ result // Return value for PHP variable type, such as String, Integer, Array $ Result = $ S-> 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 "
Corresponding Server-Diffuse: SOAP_SERVER.PHP
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:
Array. The following is a Body section of the generated SOAP:
Procedure example: soapval.php execution
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 "
WSDL
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:
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).
Example: WSDL.PHP
carried out
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:
Attached: A demo site for useful web services:
http://www.mindreef.net/soapscope/wsdldemo
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.
Section 3. XML-RPC
XML-RPC data type
XML-RPC only supports limited number of data types. Below is the corresponding relationship with the PHP data type:
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.
Installation and configuration
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
example
XML-RPC Client: XMLRPC_CLIENT.PHP Execution
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 '
'; var_dump ($ response) ; Print ' pre>';} else {//prin TERRORS Print 'error:'. $ rp-> faultcode (). ','. $ rp-> faultstring (). '
';} // show message // then let's check the message content $ MSG-> CreatePayLoad (); Print 'Request:'. $ msg-> payload. ' xmp>'; print 'response: '. $ rP-> serialize (). ' xmp > ';?> XML-RPC Server: XMLRPC_SERVER.PHP