Using PHP to implement network services _php tips

Source: Internet
Author: User
Tags soap phpinfo xmlns wso2 wsdl
Author: Samisa
A comparison of the translation names in the following text:
Payload: Conversation Content
Object: Instance
Function: Functions
Using PHP to implement network services
Use frame: WSO2 wsf/php
Installation environment: Windows or Linux
(Disgusted at the current computer article mixed with countless difficult translation and terminology, where possible use of oral and Chinese.) )
Wsmessages class:
In the process of invoking a network service, you need two messages, messages sent and messages received, and there is no way to go. Wsmessages This class is the class that encapsulates these two messages in the open source framework of the WEB Services Framework for PHP (abbreviated WSF).
Wsmessages has a very important variable STR to save the message content, to save the "payload" in XML format (they call this the payload, I look up the English dictionary, that's what it means, but it comes and go back and forth, it appears, it's the conversation content, It actually eliminates those XML definitions and some other definitions of the so-called ' namespace '->namespace. To figure out what a namespace is, look at the XML's definition of the consortium. Payload is really baffling, I later use ' conversation content ' to refer to it.
If you send a request through a client program, you need to construct an instance of Wsmessage, and fill in the example with an XML form of conversation. The response to the request, or a ' conversation ' will be returned through your program, and the returned object is still a wsmessage instance.
That is, if your client function falls on a network service, then his return value is also a wsmessage instance.
You can send a request in a function, call the Network Service program, and place the return content in the Wsmessage instance, and let the function return the Wsmessage instance.
Wsmessage is more likely to send and receive more complex content, such as attachments or something. Here's a detailed explanation of how to use Wsmessage to communicate between client and server.
Deal with conversation content:
You've already explained how to use PHP to create a Web service, and you've already done a simple client-server program to illustrate your workflow. But these programs do not explain in depth how we deal with ' conversation content '. In other words, we just sent the conversation content in XML format to the server, but did not think of dealing with it. Here, let's explain in detail how to handle the conversation and use it in a computational program.
The conversation content is defined by a business logic and is encapsulated with soap (simple Object Access Protocol) (see the article in the SOAP Consortium). Let's use an example to illustrate how to compute a factorial.
What conversations the client needs to send:
<getFactorial>
<param>6</param>
</getFactorial>
The server needs to understand the conversation and to distinguish the variables and compute its factorial. Here is the server-side 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 3rd, we created a simplexmlelement instance with the input ' conversation '. As you can see, the input of the conversation is saved to the Wsmessage instance passed in by the function argument $message the str variable. Note: simplexml is an extension of PHP that handles XML files or strings. WSO2 wsf/php does not specify which PHP extensions we must use to process XML. You can use your favorite people and XML PHP extensions to handle things like DOMDocument, Saxdom, and so on.
The 4th line extracts the parameter values from the conversation, which means that the service program needs to know how to understand these parameters, such as parameter types. (The type of this parameter is normally required to be described in the conversation content). The rest of the function is the normal processing factorial. In line sixth, factorial is computed by calling other functions. From 8 to 12 lines, the reply is also written and returned to this content. 14th Line We return to the conversation content of the reply.
Reply to the conversation should be similar to the content of this:
<getFactorialResponse>
<result>720</result>
</getFactorialResponse>
Similarly, the client can handle the conversation content in the same way:
$response = $client-> request ($reqestPayloadString);
$simplexml = new SimpleXMLElement ($response-> str);
echo "result =". $simplexml-> result [0]. "<br/>";
In line 3rd, a simplexmlelement instance is created with the conversation content of the reply. The same $response is also an example of a wsmessage, we can access his member variable STR, which holds the XML format of the reply to the conversation content. We give it to a simplexmlelement constructor, which creates an instance of SimpleXMLElement. And then we can access the result element (or node?). element, XML can be called elements, but for the tree structure of it, the node is not too far? )
Now you should learn how to handle the content of your conversation, whether it's a client application or a service-side response.
Note: On the service side of the Getfactorial function (14 lines), you can return a wsmessage to each other instead of a reply to the conversation. You can use this small program to implement this function.
$outMessage = new Wsmessage ($responsePayloadString);
return $outMessage;
This actually means that the server-side program and the conversation content that can return XML format can also return Wsmessage instances
The complete program will be attached at the end of this article.
Trace messages
With the WSO2 WEB Services Framework for PHP, you can track SOAP messages being sent by the client, and then the client receives messages from the server (i.e. their conversation content). Network customer service class, WSClient has two functions to achieve this goal: Getlastreauest () and Getlastresponse (). After the client uses the request () function, you can use these two functions to get 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 fragment shows the content of the request () and the reply to the function implementation.
In fact, this program will almost 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 useful for studying the service of a call, especially for finding services and clients. For example, you can confirm that all clients send out messages as well as messages from the server, and that you can confirm the format of the conversation (both client and server). )
Debugging (the word is so prevalent that I do not translate it here, though my dream is that one day the program will be written in Chinese, and it is clear that this dream has become more and more distant from us.) )
The user sometimes encounters two problems using PHP WSF:
Install WSF. How can you be sure that this wsf is working properly? Well, first, you can check it by using the Phpinfo () function (if you don't know the function and how to use it, well, check the PHP manual.) You just need to create a PHP file and write these words on it, and then open it in a browser.
<?php
Phpinfo ();
?>
If all the extensions are properly installed, you'll find a project called WSF, in a table with WSF as the title, you should see the words ' wsf support '. This stuff is defined in php.ini (or, for example, I did not define it in PHP.ini but wrote a new file in/etc/php5/conf.d/, called Wsf.ini, in which all the files in this folder will be merged into PHP.ini, all if you don't find the appropriate settings in php.ini but your wsf is useless, you might as well come here and see. )
If this extension does not show up in phpinfo, then you need to find the installation Guide to study it well, and if you can't find it, send me a email:ferdinandfly@yahoo.ca.
When you successfully install, the second problem is that you don't seem to be able to make this example work correctly. Again, you need to check that some settings are correct. The first is PHP.ini records, often set the path of some log files, perhaps he does not exist or that he set the path PHP5 cannot read and write. Also, you should go to confirm that php.ini contains some script files that are readable.
If all of these are correct but WSF is not working, you can check the log file. The log file is written to the path determined by this record in Wsf.log_path. This dongdong is set in the php.ini. If he is not set, then log is in/tmp (Linux). What you need to know is that in the Windows platform, the default path is probably not there, so you have to specify a log path for him. and service-related log records in the Wsf_php_server.log, and the client-related save in Wsf_php_client.log, if your client and service host is not a machine, then all two files are on the server Oh. You can get log files of varying levels of detail by adjusting the level of records. If it is debugging, you can set it to level 4, of course, if it is a mature software, you can set to 0 (just a serious error) or 1 (error).
If you want to make sure that the conversation content (SOAP) is the format you want, you can debug with SOAP message tracking, as we said earlier.
Summarize:
In this article, I explained how wsmessage this class and how to handle conversations and use it, the client or server can get the conversation content (XML) by invoking the Wsmessage member variable of str. Usually the format of the conversation content is defined by WSDL, so it is reasonable to ask the client and server to follow the same format. In the next chapter, we'll discuss how to work together 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.