Home > php教程 > PHP开发 > Use php to implement network services

Use php to implement network services

黄舟
Release: 2016-12-14 13:36:25
Original
1121 people have browsed it

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
(hate the current computer The article contains countless difficult-to-understand translations and terminology, so try to use spoken language and Chinese here.)
WSMessages class:
In the process of calling network services, two messages are needed, the sent message and the received message, and they come and go. Being able to communicate is not. 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 looked it up in the English dictionary, that's what it means, but it appears back and forth, repeatedly, Looking at it now, the content of the conversation is actually just excluding those definitions of xml and some other so-called 'namespaces' ->namespace definitions. To understand what a namespace is, please check the W3C definition of xml) . 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 previously 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 distinguish the variables and calculate its factorial. The following is the server program:
function getFacttorial ( $message ) {
$simplexml = new SimpleXMLElement ( $message -> str ) ;
$value = $simplexml -> param [ 0 ] ;
$result = factorial ( $ value ) ;
$responsePayloadString = <<
$result

XML;
return $response PayloadString;
}
3rd OK, 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. In 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:

720

Similarly, the client can also use the same method to process the reply conversation content:
$response = $client -> request ( $reqestPayloadString ) ;
$simplexml = new SimpleXMLElement ( $response -> str ) ;
echo "Result = " . $simplexml -> result [ 0 ] . "
" ;
On line 3, use the reply conversation The content creates a SimpleXMLElement instance. 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 deal with the conversation information Content, whether it is the client's application or the server's response.
Note: In the server-side getFactory function (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
The complete program will be at the end of this article attach.
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 program fragment will display the request implemented by the request() function and the content of the reply.
In fact, this program will output something like this:
Request = 6
Response = 720< /result>
Tracing SOAP messages is very useful for studying the called services, especially for finding service and client bugs. 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, so I am I won’t translate it here, although my dream is that one day programs 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:
Installation. 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 in it 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, you should see 'wsf Words like 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 the 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 (xml) by calling the str member variable of WSMessage. 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 to work together with WSF/PHP and WSDL through WSO2.

For more related articles, please pay attention to the php Chinese website (www.php.cn)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template