Learn some notes on PHP SOAP extensions

Source: Internet
Author: User
Tags soapui
The related study of SOAP first tells this paragraph, this is the last article to record the learning process of some notes and experience.

The previous three articles were:

  • "Introduction to SOAP"

  • "Introduction to SOAP Web Services"

  • Use of the PHP SOAP extension

  • How to understand

    Because the SOAP Web service is based on the HTTP protocol, it emits a SOAP message request, actually takes advantage of the POST in the HTTP verb, and then places the SOAP message inside the HTTP body. To put it simply: every time a SOAP service is invoked, a POST request is sent.

    The following is what the request interface sends:

    Post/webservices/qqonlinewebservice.asmx Http/1.1host:www.webxml.com.cnconnection:keep-aliveuser-agent:php-soap /5.4.29content-type:application/soap+xml; Charset=utf-8; action= "Http://WebXml.com.cn/qqCheckOnline"
     
      
     
          
      
       
        content-length:247        
       
                    
        
         
          
         8698053
        
                 
       
            
      
       
     
      

    As we can see, a SOAP request, in effect, sends a POST request to the server side. The content that is sent is the SOAP message (which indicates the interface method and parameters that you call this time, and so on).

    This POST request, there are some features, such as: it sends the content type: Application/soap+xml, the user agent for php-soap/5.4.29, other characteristics of their own observation, not much to say.

    The role of SOAP extensions

    In fact, since we know that the request for an interface is just a POST request, we can use some tools or some library of PHP itself (such as curl, fsockopen) to simulate sending a POST request without using PHP's SOAP extension. Yes, that's right! It is true that many people do this before PHP has yet to provide SOAP extensions.

    Why would you want to extend it with SOAP? Because it is the official website of the Bai, because it is written in C, the speed of the lever, and the package is very useful, do not need to write tedious XML code, so use it.

    In other words, the SOAP extension is actually a better, faster, HTTP wrapper library dedicated to handling the SOAP service, with nothing esoteric.

    How to use

    The PHP help manual, which has a detailed documentation of the SOAP extensions, has been very clear on how to use them, and some of the user's comments and contributions to the code snippet behind the special manual can basically solve most of your problems. Here is a list of some of the problems that you have encountered during the development process, as well as solutions and some points to note.

    WSDL and NON-WSDL

    Now basically all SOAP WEB services provide WSDL interface profiles, so non-wsdl this pattern is largely out of consideration.

    About SOAP versions

    The PHP SOAP extension supports both SOAP 1.1 and SOAP 1.2 two versions. In general, now the interface basically also supports the two SOAP protocol versions to communicate, in this case, of course, the use of a high version of SOAP 1.2. In fact, no matter which version you are using, if you are using a SOAP extension to invoke the service, there is no difference in using the extension, which is the same for you. The only thing you need to do is to set the soap_version to Soap_1_1 or soap_1_2 when the soapcient is initialized, as follows:

    SOAP 1.1 $client = new SoapClient ($wsdl, [    ' soap_version ' = + soap_1_1]);//Soap 1.2$client = new SoapClient ($WSD L, [    ' soap_version ' = soap_1_2]);

    Soapparam and Soapvar

    As mentioned above, now the service basically provides WSDL description file, if so, these two classes Soapparam and soapvar you basically can not control, because SOAP provides these two classes, mainly for the PHP SOAP extension can go to use some without WSDL Description of the service of the file, of course, this situation basically no longer exists.

    About the __soapcall method

    This method is the same, generally it will only be used in NON-WSDL mode, because in the WSDL mode, you need to call the method as a SoapClient object can be called a method. However, if the URI of the calling method is not the same as the default URI, or when the method is called, you must use the __soapcall method when you have to bring a SOAP Header to it. Here is a passage from the Official Handbook:

    The IS-a low-level API function, which is used to make a SOAP call. Usually, in WSDL mode, SOAP functions can is called as methods of the SoapClient object. This method was useful in NON-WSDL mode when SOAPAction was unknown, URI differs from the default or when sending and/or rec Eiving SOAP Headers.

    There are some differences between the use of __soapcall and the way directly called through the method name. Let's take a look at a few examples below. In the same way, we use a SOAP service that is free to use on a network to work with us, and the main function of the service is to query the online status of the user through the QQ number. Service Address

    The following is the SOAP message that should be sent when the service is requested:

     
      
     
          
      
               
       
                    
        
         
          
         String
        
                 
       
            
      
       
     
      

    This interface is called through the method name:

     
      Soap_1_2]); $client->qqcheckonline ([    ' Qqcode ' + 8698053]));

    Call the interface using the __soapcall method:

     
      Soap_1_2]); $client->__soapcall (' Qqcheckonline ', [    [' qqcode ' = 8698053]]);

    When you focus on the two methods of invocation, the parameters are different. A parameter is a one-dimensional array, as is called directly by the method name, and when called by the __soapcall method, the parameter is a two-dimensional array, which is one of the differences between them.

    The second difference is that the __soapcall method can add additional SOAP headers when invoking an interface, such as:

    $wsdl = ' http://www.example.com/service.asmx?wsdl '; $client = new SoapClient ($wsdl, [    ' soap_version ' = = soap_1_2 ]); $auth = [' sauthenticate ' = ' ab3cde34f5r4545g ']; $namespace = ' http://www.example.com '; $header = new SoapHeader ($ namespace, ' Authenheader ', $auth, false); $client->__soapcall ("SomeFunction", $parameters, NULL, $header);

    Although SoapClient also has a __setsoapheaders method, it adds a SOAP header to all methods of the instance, and if there are some methods that require a SOAP header and some do not, then you must use the __soapcall method To add a SOAP Header for a method.

    About Namespaces (namespace)

    In fact, in WSDL mode, if you do not need to send the SOAP Header, then namespace is not used, because namespace is actually already described in the WSDL file, PHP's SOAP extension will automatically parse it from the WSDL file To construct a SOAP request. If you need to add a soap Header in a SOAP message, you must provide namespace. As an example:

    For example, there is a service that requires you to send a SOAP header in a SOAP message, as in the following:

     
      
     
          
      
                           
       
        
         
        string
       
                    
      
           
      
               
       
                    
        
         
          
         int
        
                 
       
            
      
       
     
      

    The following is the code that constructs the SOAP request:

     
      Soap_1_2]); $auth = [' sauthenticate ' = ' ab3cde34f5r4545g ']; $namespace = ' http://www.example.com '; $header = new SoapHeader ($namespace, ' Authenheader ', $auth, false); $client->__setsoapheaders ($header);    $response = $client->getuserinfobyid ([    ' UserID ' = 100]);

    As you can see, when you use SoapHeader to build a SOAP Header, you must provide namespace and be the correct namespace.

    In fact, the method of constructing a soap Header is more than this one, and there are other ways to do so, such as you can also construct the same SOAP message as above:

     
      Sauthenticate = $auth;    }} $wsdl = ' http://www.example.com/service.asmx?wsdl '; $client = new SoapClient ($wsdl, [     ' soap_version ' = = soap_1_2 ]); $auth = ' ab3cde34f5r4545g '; $namespace = ' http://www.example.com '; $authenHeader = new Authenheader ($auth); $header = New SoapHeader ($namespace, ' Authenheader ', $authenHeader, false); $client->__setsoapheaders ($header); $response = $ Client->getuserinfobyid ([     ' UserID ' = + 100]);

    For more usage of SoapHeader, it is recommended to flip through the SOAP chapters in the PHP manual.

    About SoapFault

    The service side throws an SoapFault exception when processing a client request error. For SOAP extensions, which methods may throw exceptions can be viewed in the manual. Once an exception occurs, we should all capture them and handle them properly. Like this:

    try {    $client = new SoapClient ($wsdl, [        ' trace ' = True,        ' soap_version ' = = soap_1_2    ]);    .....} catch (SoapFault $e) {    //handle exception here}

    Getlastrequest and Getlastresponse

    These two methods can view the contents of the most recent request and response, which is helpful for debugging. Of course, these two methods will only take effect if the trace parameter is set to True when the soapclient is instantiated. For example, like this:

     
      True,    ' soap_version ' = soap_1_2]); $client->qqcheckonline ([    ' qqcode ' = 8698053]); Echo $client- >__getlastrequest (); Echo $client->__getlastresponse ();

    The contents of the Getlastrequest () output:

     
      
     
          
      
               
       
                    
        
         
          
         8698053
        
                 
       
            
      
       
     
      

    The contents of the Getlastresponse () output:

     
      
     
          
      
               
       
                    
        
         
          
         Y
        
                 
       
            
      
       
     
      

    SoapUI Debugging Tools

    When debugging the SOAP service interface, we can use the powerful SoapUI tool, which makes it easy to debug the interface.

    Summarize

    The above are some of their own in the study of PHP SOAP extension of some scattered notes, if there is no place, I hope you point out, thank you. (This document has been archived on GitHub)

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