PHP comes with soap extension to call Web Service

Source: Internet
Author: User
Tags object serialization
When talking about PHP Web service calls, people in the PhP4 age will immediately think of using nusoap to call the service. This nusoap is a good thing, but it has not been updated for a long time. It is feared that there will be many vulnerabilities, it's already in the PhP5 era. It's not quick to call the built-in extensions of web service!
Prerequisite: Enable PhP5 Web Service Extension
Environment requirements:
This extension makes use of the gnome XML library. Download and install this library. You will need at least libxml-2.5.4.

In Linux,
This extension is only available if PHP was configured-- Enable-soap
In the php. ini file, remove the comments from extension = php_soap.dll.

Second, call practice. The following is a summary of recent calls.

Excerpt from the manual:
Soapclient->__ soapcall ()
Class soapclient {

mixed __soapCall ( string function_name, array arguments [, array options [, mixed input_headers [, array &output_headers]]] )

This is a low level API function that is used to make a SOAP call. Usually, in WSDL mode, you can simply call SOAP functions as SoapClient methods. This method useful in non-WSDL mode when soapaction is unknown, uri differs from the default or when sending and/or receiving SOAP Headers.

On error, a call to a SOAP function can cause PHP to throw exceptions or return a SoapFault object if exceptions are disabled. To check if the function call failed to catch the SoapFault exceptions, check the result with is_soap_fault().

Return Value
Soap functions may return one, or multiple values. if only one value is returned by the soap function, the return value of _ soapcall will be a simple value (e.g. an integer, a string, etc ). if multiple values are returned, _ soapcall will return an associative array of named output parameters.

Example 1. soapclient->__ soapcall () Examples


$client = new SoapClient("some.wsdl");
$client->SomeFunction($a, $b, $c);

$client->__soapCall("SomeFunction", array($a, $b, $c));
$client->__soapCall("SomeFunction", array($a, $b, $c), NULL,
                    new SoapHeader(), $output_headers);

$client = new SoapClient(null, array('location' => "http://localhost/soap.php",
                                     'uri'      => "http://test-uri/"));
$client->SomeFunction($a, $b, $c);
$client->__soapCall("SomeFunction", array($a, $b, $c));
$client->__soapCall("SomeFunction", array($a, $b, $c),
                    array('soapaction' => 'some_action',
                          'uri'        => 'some_uri'));


1. in WSDL mode: the Web Service of the soapcall application. In this example, the web service of is used to provide the service. on the asmx page, calling and viewing are relatively simple. The example in the manual is mostly of this type, which is relatively simple.

Protocol sent by soap:

POST /servicepath/service.asmx HTTP/1.1
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: ""

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="" xmlns:xsd="" xmlns:soap="">
    <ServiceMethod xmlns="">

Call method:
$ Client = new soapclient (" WSDL ");
// Send parameter values to the soap service provider
$ Param1 = "p1 ";
$ Param2 = "p2 ";
$ Param3 = "P3 ";

// Serviceparam1, serviceparam2, and serviceparam3 are the parameter names (or field names provided by the Service) corresponding to the sent parameter values)
$ Param = array ('serviceparam1' => $ param1, 'serviceparam2' => $ param2, 'serviceparam3' => $ param3 );

// The method name is servicemethod, and the parameter array is $ Param. By default, the parameter array marked with the parameters field is passed.
$ Arr = $ client->__ soapcall ('servicemethod', array ('parameters '=> $ PARAM ));
Print_r ($ ARR );

2. In non-WSDL mode, in this case, soapaction is unknown

Soap sending protocol

POST /services/SoapMethod?WSDL HTTP/1.1
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.2.5
Content-Type: text/xml; charset=utf-8
SOAPAction: "urn:SoapMethod#ServiceMethod"
Content-Length: 1297

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="" xmlns:ns1="urn:SoapMethod" xmlns:xsd="" xmlns:xsi="" xmlns:SOAP-ENC="" xmlns:ns2="" SOAP-ENV:encodingStyle="">
<ServiceMethodSection xsi:type="ns2:ServiceObjectType">
<param1 xsi:type="xsd:string">01019</param1>
<param2 xsi:type="xsd:long">10</param2>
<param3 xsi:type="xsd:long">0</param3>
<param4 xsi:type="xsd:long">11</param4>

Call method:
2.1 pass a parameter:
Try {
$ Client = new soapclient (null,
Array ('location' => 'HTTP: // 8088/services/soappage? WSDL ', 'uri' => 'HTTP: // 8088/services /'));
$ Result = $ client->__ soapcall ('servicemethod', array ('fieldname' => "data"); // pass Params as an array
// $ Result = $ client->__ soapcall ('servicemethod', array (New soapparam ("data", 'fieldname '))); // construct parameters in the form of server parameters and pass them to the server
Var_dump ($ result );
Catch (exception $ E)
Printf ("message = % s", $ e->__ tostring ());

Appendix: manual explanation
Class soapparam {

__construct ( mixed data, string name )


The data to pass or return. You can pass this parameter directly as PHP value, but in this case it will be named as paramN and the SOAP Service may not understand it.

The parameter name

2.2 pass multiple parameters:
If the server requires that an object parameter be passed in non-WSDL and the object contains multiple attributes, the Code is as follows:

Try {
$ Client = new soapclient (null, array ('location' => 'HTTP: // 8088/services/soappage? WSDL ', 'uris' => 'urn: soapname'); // The URI part may also be the URI address.

      class Obj{ 
       public $param1 = '01019';
       public $param2 = 10;
       public $param3 = 0;
       public $param4 = 11;

$ Struct = new OBJ (); // create the object to be passed by the server
// If the server is abnormal and some parameter types of the passed parameters are not in your programming language, such as using PHP, there is no Java long type (generally, the universality should be taken into account for Web Services. The data types are both string or Int type, and I don't think it is necessary to use the long type at all. There is no way for the server to not change, but I can only change it here) the parameter must be forced type conversion processing, this process only replaces the XML parameter type with the type name required by the server in the soap transmission, and is not the data type actually transferred.
$ Struct-> param1 = iconv ('gb2312', 'utf-8', $ struct-> param1 );
$ Struct-> param2 = new soapvar ($ struct-> param2, xsd_long );
$ Struct-> param3 = new soapvar ($ struct-> param3, xsd_long );
$ Struct-> param4 = new soapvar ($ struct-> param4, xsd_long );

// For the soapvar Method Used in serialized objects, refer to the soapvar interpretation in the PHP manual. Each parameter is clearly explained.
$ Soapstruct = new soapvar ($ struct, soap_enc_object, "serviceobjecttype", ""); // Object serialization, note distinction, soap Object serialization is not using serialize

$ Result = $ client-> servicemethod (New soapparam ($ soapstruct, 'servicemethodcol '));
// $ Result = $ client->__ soapcall ('servicemethod', array (New soapparam ($ soapstruct, 'servicemethodsection ')));

Var_dump ($ result );
} Catch (exception $ e ){
Printf ("message = % s", $ e->__ tostring ());

Appendix: Explanation of soapvar found in pear Manual
Soapvar -- changes the returntype of a Variable

new SoapVar (mixed variable, long type)

This function is EXPERIMENTAL. That means, that the behaviour of this function, the function name, in concreto ANYTHING documented here can change in a future release of this package WITHOUT NOTICE. Be warned, and use this function at your own risk.
You can change the return type of variable - so that it works better with non-PHP applications for example.

type should be one of the following


Httpresolution. rar
Soap .rar

Related 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: 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.