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
Windows:
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 ()
Description
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
Example 1. soapclient->__ soapcall () Examples

<?php

$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 Asp.net 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
Host: 211.186.1.4
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://211.186.5.15/Service/ServiceMethod"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ServiceMethod xmlns="http://211.186.5.15/Service">
      <param1>string</param1>
      <param2>string</param2>
      <param3>string</param3>
    </ServiceMethod>
  </soap:Body>
</soap:Envelope>

Call method:
$ Client = new soapclient ("http://www.xxx.com/service/service.asmx? 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
Host: 220.211.1.12:8088
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="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:SoapMethod" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="http://220.211.1.12" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:ServiceMethod>
<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>
</ServiceMethodSection>
</ns1:ServiceMethod>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Call method:
2.1 pass a parameter:
Try {
$ Client = new soapclient (null,
Array ('location' => 'HTTP: // 192.168.1.180: 8088/services/soappage? WSDL ', 'uri' => 'HTTP: // 192.168.1.180: 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 )

}
Parameters

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

name
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: // 192.168.1.180: 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", "http://soapinterop.org/xsd"); // 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
Soapvar -- changes the returntype of a Variable
Description

new SoapVar (mixed variable, long type)

Warning
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

XSD_1999_TIMEINSTANT
XSD_STRING
XSD_BOOLEAN
XSD_DECIMAL
XSD_FLOAT
XSD_DOUBLE
XSD_DURATION
XSD_DATETIME
XSD_TIME
XSD_DATE
XSD_GYEARMONTH
XSD_GYEAR
XSD_GMONTHDAY
XSD_GDAY
XSD_GMONTH
XSD_HEXBINARY
XSD_BASE64BINARY
XSD_ANYURI
XSD_QNAME
XSD_NOTATION
XSD_NORMALIZEDSTRING
XSD_TOKEN
XSD_LANGUAGE
XSD_NMTOKEN
XSD_NAME
XSD_NCNAME
XSD_ID
XSD_IDREF
XSD_IDREFS
XSD_ENTITY
XSD_ENTITYS
XSD_INTEGER
XSD_NONPOSITIVEINTEGER
XSD_NEGATIVEINTEGER
XSD_LONG
XSD_INT
XSD_SHORT
XSD_BYTE
XSD_NONNEGATIVEINTEGER
XSD_UNSIGNEDLONG
XSD_UNSIGNEDINT
XSD_UNSIGNEDSHORT
XSD_UNSIGNEDBYTE
XSD_POSITIVEINTEGER



Httpresolution. rar
Soap .rar

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.