Use php to Implement Network Services

Source: Internet
Author: User
Tags wso2

Author: samisa
The following table lists the translation names:
Payload: conversation content
Object: instance
Function: function
Use php to Implement Network Services
Framework: WSO2 WSF/PHP
Installation environment: windows or linux
(I hate Computer Articles with countless difficult translations and terms. I try to use both spoken and Chinese languages here .)
WSMessages class:
In the process of calling the network service, two messages, sent messages and received messages, can be sent and received. WSMessages is a class used to encapsulate the two messages in the open-source framework Web services framework for php (WSF for short.
WSMessages has a very important variable str to save the message content and save the "payload" in xml format (they call this payload. I look up the English dictionary, which means that, however, it appears back and forth repeatedly. In today's view, that is, the conversation content, it actually removes the xml definitions, and some other so-called 'namespace '-> namespace definitions. To understand what a namespace is, see the W3C definition of xml ). The payload is really inexplicable. I will refer to it with 'conversation content' later.
If you send a request through a client program, you need to construct a WSMessage instance and enter the instance in xml format. The response of the corresponding request is also a 'conversation content' which will be returned through your program, and the returned content is still a WSMessage instance.
That is to say, if your client function falls into a network service, the return value is also a WSMessage instance.
You can send a request in a function, call the program of the network service, and put the returned content in the WSMessage instance, and ask the function to return the WSMessage instance.
WSMessage is more inclined to send and accept complicated content, such as attachments. The following describes how to use WSMessage to communicate between the client and the server.
Process conversation content:
Before that, I have explained how to use php to create a network service and a simple client-server program to describe the workflow. However, these programs do not explain how to deal with 'conversation content' in depth '. In other words, we only sent the conversation content in xml format to the server, but we didn't want to process it. Here, we will explain in detail how to deal with the conversation content and use it in computing programs.
The conversation content is defined by business logic and encapsulated by SOAP (Simple Object Access Protocol). (See the SOAP w3c article ). Let's use an example to illustrate how to calculate a factorial.
The conversation content that the client needs to send:
<GetFactorial>
<Param> 6 </param>
</GetFactorial>
The server needs to understand the conversation content, distinguish the variables, and calculate their factorial. The following is the server program:
Function getFactorial ($ message ){
$ Simplexml = new SimpleXMLElement ($ message-> str );
$ Value = $ simplexml-> param [0];
$ Result = factorial ($ value );
$ ResponsePayloadString = <XML
<GetFactorialResponse>
<Result> $ result </result>
</GetFactorialResponse>
XML;
Return $ responsePayloadString;
}
Line 3: We used the entered 'conversation content' to create an instance of simpleXmlElement. As you can see, the input conversation content is saved to the str variable of the WSMessage instance $ message passed in through function parameters. Note: SimpleXml is a php extension used to process xml files or strings. WSO2 WSF/PHP does not specify which php extension we must use to process xml. You can use your favorite and xml php extensions, such as domdocument and saxdom.
The 4th line extracts the parameter values from the conversation content, which means that the service program needs to know how to understand these parameters, such as parameter types. (Normally, you need to describe the type of this parameter in the conversation content ). The rest of the function is the normal processing of factorial. In the sixth row, the factorial is calculated by calling other functions. From 8 to 12 rows, the reply conversation content is also written and will be returned. In row 3, we return the reply conversation content.
The reply conversation content should be similar to the following:
<GetFactorialResponse>
<Result> 720 </result>
</GetFactorialResponse>
Similarly, the client can also process the reply conversation content in the same way:
$ Response = $ client-> request ($ reqestPayloadString );
$ Simplexml = new SimpleXMLElement ($ response-> str );
Echo "Result =". $ simplexml-> result [0]. "<br/> ";
In row 3, a SimpleXMLElement instance is created with the reply conversation content. The same $ response is also a WSMessage instance. We can access its member variable str, which saves the conversation content in xml format. We give it to a SimpleXMLElement constructor and create an instance of SimpleXMLElement. Then we can access the result element (or node? Element, which can be called an element in xml, but for a tree structure, the node is not too old? )
Now you should learn how to deal with the content in the conversation information, whether it is a client application or a server response.
Note: In the getFactorial function (line 14) on the server side, you can return a WSmessage instead of a reply conversation. You can use the following short program to implement this function.
$ OutMessage = new WSMessage ($ responsePayloadString );
Return $ outMessage;
In fact, this means that the server program and the conversation content that can return xml format can also return the WSMessage instance.
The complete program will be included at the end of this article.
Tracking messages
With WSO2 Web services framework for PHP, you can track the SOAP message sent by the client, and then the client receives the message from the server (that is, their conversation content ). In the network customer service class, WSClient has two functions for this purpose: getLastReauest () and getLastResponse (). After the client uses the request () function, you can use these two functions to obtain the conversation information.
$ Response = $ client-> request ($ reqestPayloadString );
Printf ("<br/> Request = % s </br> ",
Htmlspecialchars ($ client-> getLastRequest ()));
Printf ("<br/> Response = % s </br> ",
Htmlspecialchars ($ client-> getLastResponse ()));
The above program snippet will display the request and response content implemented by the request () function.
In fact, this program will output something like this:
Request = <soapenv: Envelope xmlns: soapenv = "http://www.w3.org/2003/05/soap-envelope"> <soapenv: Header/> <soapenv: body> <getFactorial> <param> 6 </param> </getFactorial> </soapenv: Body> </soapenv: Envelope>
Response = <soapenv: Envelope xmlns: soapenv = "http://www.w3.org/2003/05/soap-envelope"> <soapenv: Header/> <soapenv: body> <getFactorialResponse> <result> 720 </result> </getFactorialResponse> </soapenv: Body> </soapenv: Envelope>
Tracking SOAP messages is very useful for researching call services, especially for finding bugs in services and clients. For example, you can confirm all the messages sent by the client and the messages replied by the server, and you can confirm the format of the conversation content (the client and the server. )
Debugging, obviously, this dream is getting farther and farther away from us. )
Users may encounter two problems when using php WSF:
Install wsf. How can you determine that this wsf is working properly? Well, first, you can check through the phpinfo () function. (If you don't know about this function and how to use it, check the php manual. ) You only need to create a PHP file, write these words on it, and then open it in a browser.
<? Php
Phpinfo ();
?>
If all the extensions are correctly installed, you will find a project named wsf. In a table titled wsf, you should see words like 'wsf support. This stuff is in php. as defined in ini (or, for example, I am not in php. in ini, It is defined in/etc/php5/conf. d/write a new file named wsf. ini. In fact, all the files in this folder will be merged to php later. ini, if you are not in php. you can find the corresponding settings in ini but your wsf is insufficient. )
If this expansion is not shown in phpinfo, then you need to find the Installation Guide to study, if you can not find me email: ferdinandfly@yahoo.ca
After successful installation, the second problem is that you do not seem to be able to correctly run this example. Similarly, you need to check whether some settings are correct. First, some log file paths are often set in the php. ini record. Maybe the path does not exist or php5 cannot be read or written. Also, check whether php. ini contains some script files that are readable.
If the above information is correct, but wsf does not work, you can check the log file. The log file is written to the path specified by the record wsf. log_path. This stuff is set in php. ini. If it is not set, log is in/tmp (linux ). You need to know that the default path may not exist on windows, so you must specify a Log Path for it. Service-related logs are recorded in wsf_php_server.log, and client-related logs are stored in wsf_php_client.log. If your client and service host are not one machine, these two files are all stored on the server. You can adjust the record level to obtain log files of different levels. For debugging, you can set it to level 4. For mature software, you can set it to 0 (only serious errors) or 1 (errors ).
If you want to confirm that the conversation content (SOAP) is in the format you want, you can use SOAP message tracing for debugging, as we mentioned earlier.
Summary:
In this article, I explained the WSMessage class and how to handle the conversation content and use it, the client or server can call the WSMessage member variable str to obtain the conversation content (xml ). Generally, the format of the conversation content is defined by WSDL. Therefore, it is reasonable for the client and server to follow the same format. In the next chapter, we will discuss how to work collaboratively through WSO2 WSF/PHP and WSDL.

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.