Notes on PHPSOAP extensions

Source: Internet
Author: User
Tags soapui
Let's take notes on PHPSOAP extensions and learn about SOAP first. this is the last article used to record some notes and experiences in the learning process.

The first three articles are:

  • Introduction to SOAP

  • Introduction to SOAP Web services

  • Use of php soap extensions

  • How to Understand

    Because the SOAP Web service sends a SOAP message request based on the HTTP protocol, it actually uses the POST in the HTTP verb and then places the SOAP message to the HTTP body for sending. Simply put, each time a SOAP service is called, a POST request is sent.

    The following is the content sent by a request interface:

    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
                
           
      
     

    We can see that a SOAP request actually sends a POST request to the server. The sent content is exactly the SOAP message (it indicates the interface method and parameters you call this time ).

    This POST request has some features, such as: It sends the Content Type: application/soap + xml, the user agent for PHP-SOAP/5.4.29, other features observed by yourself, not much said.

    Functions of SOAP extension

    In fact, since we know that one request is only one POST request, we can use some tools or libraries that come with PHP (such as curl and fsockopen) to simulate POST requests, php soap extensions are not required. Yes, that's right! Many people did the same before PHP provided SOAP extensions.

    Why do we need to use SOAP extensions? Because it is written in C language on its official website, it is highly efficient and well encapsulated. you don't need to write complicated XML code yourself, so you can use it.

    In other words, in fact, SOAP extension is a more useful and faster HTTP encapsulation Library dedicated for processing SOAP services. there is nothing very esoteric.

    Usage

    PHP help manual, detailed instructions on SOAP extensions, has been made clear about how to use them, and some user comments and contributed code snippets later in the special manual, basically, most of your problems can be solved. Below we will record some of the problems we encountered during the development process, as well as the solutions and precautions.

    WSDL and non-WSDL

    Currently, basically all SOAP Web services provide the WSDL interface description file. Therefore, the non-WSDL mode is not considered.

    About the SOAP Version

    Php soap extensions support both SOAP 1.1 and SOAP 1.2 versions. In general, the current interface basically supports both SOAP protocol versions for communication, so in this case, of course, the higher version of SOAP 1.2 is used. In fact, no matter which version you use, if you use a SOAP extension to call a service, there is no difference in using this extension, and it is the same for you. The only thing you need to do is set soap_version to SOAP_1_1 or SOAP_1_2 during SoapCient initialization, as shown below:

    // SOAP 1.1 $client = new SoapClient($wsdl, [    'soap_version' => SOAP_1_1]);// SOAP 1.2$client = new SoapClient($wsdl, [    'soap_version' => SOAP_1_2]);
    SoapParam and SoapVar

    As mentioned above, the current service basically provides the WSDL description file. if so, you can skip the two classes SoapParam and SoapVar, SOAP provides these two classes mainly for php soap extensions to use services without WSDL description files. of course, this situation basically does not exist.

    About the _ soapCall method

    This method is also the same. generally, it is only used in the non-WSDL mode, because in the WSDL mode, you can call the method you need to call as a method of the SoapClient object. However, if the uri of the call method is different from the default uri, or when the method is called, you must use the _ soapCall method when you need to add a SOAP Header to it. The following is an excerpt from the official manual:

    This is a low level API function that is used to make a SOAP call. usually, in WSDL mode, SOAP functions can be called as methods of the SoapClient object. this method is useful in non-WSDL mode when soapaction is unknown, uri differs from the default or when sending and/or refreshing SOAP Headers.

    _ SoapCall is different from the method called directly by method name. Let's take a look at several examples. Similarly, we still use a SOAP service that can be used free of charge on the network to cooperate with us. The main function of this 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 requesting the service:

     
         
              
                   
        
         string
                
           
      
     

    Call this interface by method name:

      SOAP_1_2]);$client->qqCheckOnline([    'qqCode' => 8698053]));

    Use the _ soapCall method to call this interface:

      SOAP_1_2]);$client->__soapCall('qqCheckOnline', [    ['qqCode' => 8698053]]);

    The parameters vary depending on the two call methods. You can directly call a method by using the method name. The parameter is a one-dimensional array. when you call 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 calling interfaces, for example:

    $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 the _ setSoapHeaders method, it adds the SOAP Header to all methods of the instance. if some methods require the SOAP Header and some do not need it, you must use the _ soapCall method to add a SOAP Header to a method.

    Namespace)

    In fact, in the WSDL mode, if you do not need to send SOAP headers, namespace cannot be used because namespace is already described in the WSDL file, PHP's SOAP extension automatically parses it from the WSDL file to construct a SOAP request. If you need to add a SOAP Header to a SOAP message, you must provide a namespace. For example:

    For example, a service that requires you to send a SOAP message must contain a SOAP Header, as shown below:

     
         
                          
       
        string
                   
          
              
                   
        
         int
                
           
      
     

    The following code 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]);

    We can see that when using SoapHeader to construct a SOAP Header, namespace must be provided and the namespace is correct.

    In fact, there are more than one way to construct a SOAP Header. for example, 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 SoapHeader usage, read the SOAP section in the PHP Manual.

    About SoapFault

    When the server encounters an error in processing client requests, it will throw a SoapFault exception. For methods that may throw exceptions in SOAP extensions, refer to the manual. Once exceptions occur, we should capture them and properly handle them. As shown below:

    Try {$ client = new SoapClient ($ wsdl, ['Trace '=> true, 'soap _ version' => SOAP_1_2]); ......} catch (SoapFault $ e) {// handle exceptions here}
    GetLastRequest and getLastResponse

    These two methods can be used to view the content of the last request and response. These two methods are very helpful for debugging. Of course, these two methods take effect only when the trace parameter is set to true when the SoapClient is instantiated. For example:

      true,    'soap_version' => SOAP_1_2]);$client->qqCheckOnline([    'qqCode' => 8698053]));echo $client->__getLastRequest();echo $client->__getLastResponse();

    Content output by getLastRequest:

     
         
              
                   
        
         8698053
                
           
      
     

    Content output by getLastResponse:

     
         
              
                   
        
         Y
                
           
      
     
    SoapUI debugging tool

    When debugging a SOAP service interface, we can use a powerful SoapUI tool to easily debug the interface.

    Summary

    The above are some scattered notes when you are learning php soap extensions. if there is something wrong, I hope you can point it out. thank you. (GitHub has been archived in this article)

    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.