Using PHP to implement Web services _php tutorials

Source: Internet
Author: User
Tags comparison table soap phpinfo wso2
Author: Samisa
The translation name comparison table in the following article:
Payload: What to talk about
Object: Instance
Function: Functions
Using PHP to implement Web services
Frame of use: WSO2 wsf/php
Installation environment: Windows or Linux
(Disgust at the moment the computer articles are mixed with countless translations and terminology, and use spoken and Chinese as much as possible here.) )
Wsmessages class:
In the process of invoking a network service, two messages are required, messages are sent and messages are received, and there is no way to go. Wsmessages This class is the class used to encapsulate 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, in XML format to save the "payload" (they call this payload, I check the English dictionary, this is the meaning, but it appears back and forth, repeated appearance, this view, that is, the conversation content, is actually to remove those definitions of XML, as well as some other so-called ' namespace '->namespace definitions. To figure out what a namespace is, look at the definition of XML. Payload is really baffling, I still use ' talk content ' to refer to it.
If you send a request through the client, you need to construct an instance of Wsmessage, and fill in the example with an XML form of the conversation. The response to the request, or a ' talk content ' will be returned through your program, and the returned item is still a wsmessage instance.
That is, if your client function falls out of a network service, then his return value is also a wsmessage instance.
You can send the request in a function, invoke the Network Service's program, and put the return content in the Wsmessage instance, and let the function return the Wsmessage instance.
Wsmessage is more inclined to send and receive more complex content such as attachments or something. Here's a detailed explanation of how to use Wsmessage for client and server communication.
Deal with the conversation content:
Before that, we've explained how to use PHP to create Web services, and have done a simple client-server program to illustrate the workflow. But these programs do not explain in depth how we deal with ' conversations '. In other words, we just sent the XML format of the conversation to the server, but did not expect to deal with it. Here, let's explain in detail how to deal with the conversation and use it in the computational program.
The conversation is defined by a business logic and encapsulated with soap (simple Object Access Protocol) (see the SOAP articles article). Let's use an example to illustrate how to calculate a factorial.
What the client needs to send the conversation:

6

The server needs to understand the conversation and identify the variable and calculate its factorial. Here is the server-side program:
function Getfactorial ($message) {
$simplexml = new SimpleXMLElement ($message-STR);
$value = param ($simplexml) [0];
$result = factorial ($value);
$responsePayloadString = <<
$result

XML;
return $responsePayloadString;
}
In line 3rd, we create an instance of SimpleXMLElement with the input ' conversation content '. As you can see, the input conversation is saved to the STR variable $message the Wsmessage instance passed in through the function arguments. Note: simplexml is an extension of PHP that handles XML files or strings. WSO2 wsf/php does not specify which PHP extension we have to use to process XML. You can use your favorite people and XML PHP extensions to deal with, such as 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. (It is normal to specify the type of the parameter in the conversation). The rest of the function is the normal processing factorial. In line sixth, factorial is computed by invoking other functions. From 8 to 12 lines, replies to the conversation are also written and must be returned to this content. Line 14th we return to reply to the conversation content.
The reply should be similar to the following:

720

Similarly, the client can handle the conversation with the reply in the same way:
$response = Request ($reqestPayloadString) $client
$simplexml = new SimpleXMLElement ($response-STR);
echo "Result =". result [0], $simplexml. "
" ;
In line 3rd, a simplexmlelement instance is created with the reply-to-conversation content. The same $response is also an example of a wsmessage, where we can access his member variable STR, which holds a reply to the XML format of the conversation content. We'll give it to a simplexmlelement constructor, which creates an instance of SimpleXMLElement. Then we can access the result element (or node?). element, the XML can be called elements, but for the tree structure of it, the node is not too? )
Now you should learn how to deal with the content of the conversation message, whether it is the client's application or the response from the server.
Note: On the server side of the Getfactorial function (14 lines), you can return a wsmessage instead of a reply to the conversation content. You can use this short program to implement this function.
$outMessage = new Wsmessage ($responsePayloadString);
return $outMessage;
This means that the service-side program and the chat content that can return XML format can also return an instance of Wsmessage
The complete program will be attached at the end of this article.
Trace messages
With WSO2 WEB Services Framework for PHP, you can trace the SOAP message being sent by the client, and then the client receives a message from the server (that is, what they are talking about). Network customer service class, WSClient has two functions to achieve this purpose: getlastreauest () and Getlastresponse (). After the client uses the request () function, you can use these two functions to get the conversation information.
$response = Request ($reqestPayloadString) $client
printf ("
Request =%s
" ,
Htmlspecialchars ($client-getlastrequest ()));
printf ("
Response =%s
" ,
Htmlspecialchars ($client-Getlastresponse ()));
The above program fragment shows the request () and the content of the reply requested by this function.
In fact, this program will almost output something like this:
Request = 6
Response = 720
Tracing SOAP messages is useful for studying the services of a call, especially for locating services and client bugs. For example, you can confirm all the messages sent by the client and the server reply message, and you can confirm the format of the conversation (client and server). )
Debugging (this word is so common, I do not translate it here, although my dream is one day the program in Chinese to write, it is obvious that the dream has been more and more distant from us. )
Users sometimes encounter two problems when using PHP WSF:
Install WSF. How can you be sure that this wsf is working properly? Well, first, you can check through this function of phpinfo () (If you don't know the function and how to use it, uh, check the PHP manual.) You just need to create a PHP file and write down these words on it, and then open it with a browser.
Phpinfo ();
?>
If all the extensions are properly installed, you will find a project called WSF, in a table with WSF as the title, you should see the words ' wsf support '. This thing is defined in the php.ini, (or, for example, I did not define in the php.ini, but in the/etc/php5/conf.d/wrote a new file called Wsf.ini, in fact, all the files in this folder will later be merged into PHP.ini, all if you have not found the appropriate settings in php.ini but your wsf lack of energy, you might as well come here to see. )
If this extension does not show up in the phpinfo, then you need to find the installation Guide to do a good job, if you can not find to send me email:ferdinandfly@yahoo.ca
When you have successfully installed, the second problem is that you don't seem to be able to run the example correctly. Again, you need to check that some of the settings are correct. The first is the php.ini record, often set some log file path, perhaps he does not exist or the path he set PHP5 cannot read and write. Also, you should 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 the Wsf.log_path record. This stuff is set in 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 must specify a log path for him. Service-related logging in Wsf_php_server.log, and the client-related save in Wsf_php_client.log, if your client and service host are not a machine, then these two files are on the server Oh. You can get log files of different levels of detail by adjusting the level of records. If you are debugging, you can set it to level 4, of course, if it is a mature software, you can set it to 0 (just a serious error) or 1 (error).
If you want to make sure that the conversation (SOAP) is the format you want, you can debug it with SOAP message tracking, as we said earlier.
Summarize:
In this article, I explained the Wsmessage class and how to handle the conversation and use it, either the client or the server can get the conversation content (XML) by invoking the member variable of STR, the wsmessage. The format of conversational content is usually defined by WSDL, so it is reasonable to ask the client and the server to follow the same format. In the next chapter we will discuss how to work together through WSO2 wsf/php and WSDL.

http://www.bkjia.com/PHPjc/320471.html www.bkjia.com true http://www.bkjia.com/PHPjc/320471.html techarticle Author: Samisa translation names in the following text table: payload: Conversation Object: Instance function: Functions use PHP to implement Network Service usage framework: WSO2 wsf/php installation Environment: ...

  • 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.