Author: samisa
Comparison table of translated names in the following text:
payload: conversation content
object: instance
function: function
Use PHP to implement network services
Using framework: WSO2 WSF /PHP
Installation environment: windows or linux
(I am disgusted that computer articles are now mixed with countless difficult to understand translations and terms, so I try to use spoken language and Chinese here.)
WSMessages class:
When calling the network During the service process, two messages are required, the sent message and the received message, and they must come and go before they can come and go. The WSMessages class is used to encapsulate these 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 checked the English dictionary, that's what it means, but it appears back and forth, repeatedly Appearance, now viewed, is the content of the conversation, which actually removes those definitions of xml and some other so-called 'namespace'->namespace definitions. To find out what a namespace is, please check out the W3C of xml. definition). The payload is so baffling that I will refer to it as 'conversation content' from now on.
If you send a request through the client program, then you need to construct an instance of WSMessage and fill in the instance with the conversation content in xml form. The response to the request is still a "conversation content" that will be returned through your program, and what is returned is still a WSMessage instance.
In other words, if your client function responds to a network service, its return value is also a WSMessage instance.
You can send a request in a function, call the network service program, put the return content in the WSMessage instance, and let the function return this WSMessage instance.
WSMessage is more inclined to send and receive more complex content such as attachments. Let's explain in detail how to use WSMessage to communicate between the client and the server.
Processing the conversation content:
I have already explained how to use php to create network services, and have made a simple client-server program to illustrate the workflow. But these programs don't explain in depth how we process 'conversation content'. In other words, we just sent the conversation content in xml format to the server, but did not think of processing it. Here, we explain in detail how to process conversation content and use it in computing programs.
The conversation content is content defined by business logic and encapsulated using SOAP (Simple Object Access Protocol) (please refer to SOAP w3c article). Let's use an example to illustrate how to calculate a factorial.
Conversation content that the client needs to send:
6
The server needs to understand this conversation content and send it to The variable is resolved and its factorial is calculated. The following is the server program:
function getFactorial ( $message ) {
$simplexml = new SimpleXMLElement ( $message -> str ) ;
$value = $simplexml -> param [ 0 ] ;
$result = factorial ( $value ) ;
$responsePayloadString = <<
< /getFactorialResponse>
XML;
return $responsePayloadString;
}
In line 3, we create an instance of simpleXmlElement with the input 'conversation content'. You can see that the entered conversation content is saved to the str variable of $message of the WSMessage instance passed in through the function parameter. Note: SimpleXml is a php extension for processing xml files or strings. WSO2 WSF/PHP does not specify which php extension we must use to process xml. You can use your favorite XML and PHP extensions to handle it, such as domdocument, saxdom and the like.
Line 4 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 and the like. (Normally, the type of this parameter needs to be stated in the conversation content). The rest of the function is the normal handling of factorials. On line 6, the factorial is calculated by calling other functions. From lines 8 to 12, the reply conversation content is also written and must be returned. On line 14 we return the conversation content of the reply.
The reply conversation content should be almost like this:
Similarly, the client can also Use the same method to process the reply chat content:
$response = $client -> request ( $reqestPayloadString ) ;
$simplexml = new SimpleXMLElement ( $response -> str ) ;
echo "Result = " . $simplexml -> result [ 0 ] . "
" ;
In line 3, a SimpleXMLElement instance is created with the reply's conversation content.Similarly, $response is also an instance of WSMessage. We can access its member variable str, which stores the conversation content of the reply in xml format. We pass it to a SimpleXMLElement constructor, thereby creating an instance of SimpleXMLElement. Then we can access the result element (or node? element, it can be called an element in xml, but for a tree structure, a node is not too much?)
Now you should learn how to process chat information content, whether it is the client's application or the server's response.
Note: In the getFactory function on the server side (line 14), you can return a WSmessage instead of a reply conversation content. You can use the following short program to achieve this function.
$outMessage = new WSMessage( $responsePayloadString );
return $outMessage ;
This actually means that the server program can return the conversation content in xml format and can also return an instance of WSMessage
Complete The program will be attached at the end of this article.
Tracking messages
Through WSO2 Web services framework for PHP, you can track SOAP messages sent by the client, and then the client receives the message from the server (that is, the content of their conversation). Network client service class, WSClient has two functions to achieve this purpose: getLastReauest() and getLastResponse(). After the client uses the request() function, you can get the conversation information through these two functions.
$response = $client -> request ( $reqestPayloadString ) ;
printf ( "
Request = %s " ,
htmlspecialchars ( $client -> getLastRequest ())) ;
printf ( "
Response = %s " ,
htmlspecialchars ( $client -> getLastResponse ())) ;
the above The program fragment will display the content of the request and reply implemented by the request() function.
Actually, this program will output something like this:
Request =
Response =
Tracing SOAP messages is very useful for researching the services being called, especially for finding services and Client bug. 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 (client and server.)
Debugging (this word is so common, then I won’t translate it here, although my dream is that one day the program will be written in Chinese, it is obvious that this dream is getting further and further away from us)
Users sometimes encounter two problems when using php WSF. Question:
Install wsf. How can you be sure that this wsf is working properly? Well, first, you can check it through the phpinfo() function, (If you don't know this function and how to use it, uh, check the php manual.) You just need to create a php file and write Download these sentences and open it in a browser.
phpinfo () ;
?>
If all extensions are installed correctly, you will find an item called wsf in a table with wsf as the title There, you should see words like 'wsf support'. This stuff is defined in php.ini, (or for example, I didn’t define it in php.ini but wrote a new file called wsf.ini in /etc/php5/conf.d/. Actually All the files in this folder will be merged into php.ini later, so if you don’t find the corresponding settings in php.ini but your wsf is not available, you might as well come here and take a look)
If this extension is not displayed in phpinfo, then you need to find the installation guide to study it carefully. If you can't find it, you can send me an email: ferdinandfly@yahoo.ca
After you successfully installed it, The second problem is that you can't seem to get this example to run correctly. Likewise, you need to check that some settings are correct. The first is that in the php.ini record, the path to some log files is often set. Maybe it does not exist or the path it sets cannot be read and written by php5. Also, you should make sure that php.ini contains some script files and that these script files are readable.
If the above is correct but wsf is not working, you can check the log file. The log file will be written to the path determined by the wsf.log_path record. This stuff is set in php.ini. If it is not set, the log is in /tmp (linux).What you need to know is that on the Windows platform, the default path may not exist, so you must specify a log path for it. Logs related to the service are recorded in wsf_php_server.log, and those related to the client are stored in wsf_php_client.log. If your client and service host are not the same machine, then both files are on the server. You can obtain log files with different levels of detail by adjusting the logging level. If it is debugging, you can set it to level 4. Of course, if it is mature software, you can set it to 0 (only serious errors) or 1 (error).
If you want to confirm that the exchange content (SOAP) is in the format you want, you can use SOAP message tracing to debug, as we talked about earlier.
Summary:
In this article, I explained the WSMessage class and how to process the conversation content and use it. Either the client or the server can get the conversation content by calling the str member variable of WSMessage (xml ). Usually the format of the conversation content is defined through WSDL, so it is reasonable for us to require the client and server to comply with the same format. In the next chapter we will discuss how WSF/PHP and WSDL work together through WSO2.