Example of soap usage in PHP and PHPsoap usage

Source: Internet
Author: User

Example of soap usage in PHP and PHPsoap usage

This article describes the use of soap in PHP and shares it with you for your reference. The usage analysis is as follows:

PHP uses soap in two ways.

1. Use the wsdl File

Server:

Copy codeThe Code is as follows: <? Php
Class service
{
Public function HelloWorld ()
{
Return "Hello ";
}
Public function Add ($ a, $ B)
{
Return $ a + $ B;
}
}
$ Server = new SoapServer ('soap. wsdl ', array ('soap _ version' => SOAP_1_2 ));
$ Server-> setClass ("service ");
$ Server-> handle ();
?>
Resource Description file, which can be generated using zend studio. It is actually an xml file.
Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Wsdl: definitions xmlns: soap = "The http://schemas.xmlsoap.org/wsdl/soap/" xmlns: tns = "http: // localhost/interface/" xmlns: wsdl = "http://schemas.xmlsoap.org/wsdl/" xmlns: xsd = "http://www.w3.org/2001/XMLSchema" name = "soap" targetNamespace = "http: // localhost/interface/">
<Wsdl: types>
<Xsd: schema targetNamespace = "http: // localhost/interface/">
<Xsd: element name = "HelloWorld">
<Xsd: complexType>
<Xsd: sequence>
<Xsd: element name = "in" type = "xsd: string"/>
</Xsd: sequence>
</Xsd: complexType>
</Xsd: element>
<Xsd: element name = "HelloWorldResponse">
<Xsd: complexType>
<Xsd: sequence>
<Xsd: element name = "out" type = "xsd: string"/>
</Xsd: sequence>
</Xsd: complexType>
</Xsd: element>
<Xsd: element name = "Add">
<Xsd: complexType>
<Xsd: sequence>
<Xsd: element name = "in" type = "xsd: int"> </xsd: element>
</Xsd: sequence>
</Xsd: complexType>
</Xsd: element>
<Xsd: element name = "AddResponse">
<Xsd: complexType>
<Xsd: sequence>

<Xsd: element name = "out" type = "xsd: int"> </xsd: element>
</Xsd: sequence>
</Xsd: complexType>
</Xsd: element>
</Xsd: schema>
</Wsdl: types>
<Wsdl: message name = "AddRequest"> <wsdl: part name = "a" type = "xsd: int"> </wsdl: part>
<Wsdl: part name = "B" type = "xsd: int"> </wsdl: part>
</Wsdl: message>
<Wsdl: message name = "AddResponse">
<Wsdl: part name = "c" type = "xsd: int"> </wsdl: part>
</Wsdl: message>
<Wsdl: portType name = "TestSoap"> <wsdl: operation name = "Add">
<Wsdl: input message = "tns: AddRequest"> </wsdl: input>
<Wsdl: output message = "tns: AddResponse"> </wsdl: output>
</Wsdl: operation>
</Wsdl: portType>
<Wsdl: binding name = "soapSOAP" type = "tns: TestSoap">
<Soap: binding style = "document"
Transport = "http://schemas.xmlsoap.org/soap/http"/>
<Wsdl: operation name = "Add">
<Soap: operation soapAction = "http: // localhost/interface/Add"/>
<Wsdl: input>
<Soap: body use = "literal"
Namespace = "http: // localhost/interface/"/>
</Wsdl: input>
<Wsdl: output>
<Soap: body use = "literal"
Namespace = "http: // localhost/interface/"/>
</Wsdl: output>
</Wsdl: operation>
</Wsdl: binding>
<Wsdl: service name = "TestSoap">
<Wsdl: port binding = "tns: soapSOAP" name = "soapSOAP">
<Soap: address location = "http: // localhost/interface/myservice. php"/>
</Wsdl: port>
</Wsdl: service>
</Wsdl: definitions>
Client call:
Copy codeThe Code is as follows: <? Php
$ Soap = new SoapClient ('HTTP: // localhost/interface/soap. wsdl ');
Echo $ soap-> Add (1, 2 );
?>
2. Do not use the wsdl File

Server:

Copy codeThe Code is as follows: <? Php
Class service
{
Public function HelloWorld ()
{
Return "Hello ";
}
Public function Add ($ a, $ B)
{
Return $ a + $ B;
}
}
$ Server = new SoapServer (null, array ('uri '=> "abcd "));
$ Server-> setClass ("service ");
$ Server-> handle ();
?>
Client:
Copy codeThe Code is as follows: <? Php
Try {
$ Soap = new SoapClient (null, array (
"Location" => "http: // localhost/interface/soap. php ",
"Uri" => "abcd", // The resource descriptor server and client must correspond
"Style" => SOAP_RPC,
"Use" => SOAP_ENCODED
));

Echo $ soap-> Add (1, 2 );
} Catch (Exction $ e ){
Echo print_r ($ e-> getMessage (), true );
}
?>

I hope this article will help you with PHP programming.


Php soap is used in windows (phpini). After the variables are configured, they cannot be used.

How can we determine that he cannot use it, and that he cannot determine the cause of the error without a picture or truth (php5 and above support soap)
 
Example of soap implementation in php. For example, if a class file is stored on a server, I can use it directly on an exception server,

Try {

$ This-> soap-> = new SoapClient ("localhost/WebService. asmx? Wsdl "); // note that the c # asmx service is used here.
$ Obj = $ this-> soap-> webservice method name (array ("server parameter name" => "parameter value ", "Second server parameter name" => "second parameter value "));
Return $ this-> json_decode_CSharp ($ obj); // different resolution bodies are implemented based on different services (JSON data is returned). json_decode_csharp is my custom function.
} Catch (Exception $ e ){
$ This-> file-> vim ($ e->__ toString (), $ this-> logpath. date ("Ymd"). ". log ");
Return null;
}
You can add a php learning and communication group to the group to ask: 40383880

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.