PHP Soap Example Explained

Source: Internet
Author: User
Tags php write soap client

PHP Soap Example Explained

One, what is soap, what is WSDL, why use them

SOAP is a language supported by XML and HTTP communication protocols, XML platforms, and various languages. What about HTTP? It is supported by all Internet browsers and servers.

WSDL refers to the Network Service Description Language (Web Services Description Language), which is a document written using XML. This document can describe a WEB service. It can specify the location of the service and the operations provided by this service.

I do PHP, you are Java, he is to do. NET, if we want to communicate between the three, to exchange data, how to do? We need a tool that can communicate with all of us. SOAP,WSDL is created so that applications running on different operating systems and using different technologies and programming languages can communicate with each other.

Two, examples

If PHP is to use soap, the usual practice is to add a bit of PHP soap module, in php.ini with Soap.so, the following describes a do not add soap.so file, you can implement the SOAP method

Nusoap is a PHP write a function file, inclusion in can be used, online a lot of their own to search it.

1, do not use WSDL

A, server helloworld2.php

<?PHP//Package Letter Nusoap.php    require_once('./lib/nusoap.php '); //Create a service-side    $server=NewSoap_server; //defining Client Invocation methods    $server->register (' Hello '); //calling methods and parameters    functionHello$name) {       return' Hello, '.$name; }            $HTTP _raw_post_data=isset($HTTP _raw_post_data) ?$HTTP _raw_post_data: ‘‘; $server->service ($HTTP _raw_post_data); ?>

B, Client hello.php

<?PHP//Package Letter Nusoap.php    require_once('./lib/nusoap.php '); //Create a new SOAP client that invokes the WSDL//$client provided by the server = new SoapClient (' http://localhost/test/hellowsdl2.php?wsdl ', true);     $client=NewSoapClient (' http://localhost/test/helloworld2.php '); //Check to see if it's an error    $err=$client-GetError (); if($err) {       //Display Error     Echo' $err. ' </pre> '; }            //methods to invoke the service side    $result=$client->call (' Hello ',Array(' person ' = ' = ' This is a test ')); Echo' ; Print_r($result); Echo' </pre> '; ?>

2, using WSLD

A, server-side

<?PHP//Package Letter Nusoap.php    require_once('./lib/nusoap.php '); //Create a new soap service    $server=NewSoap_server (); //Initialize support WSDL    $server-&GT;CONFIGUREWSDL (' hellowsdl2 ', ' URN:HELLOWSDL2 '); //define the data structure to receive    $server->wsdl->Addcomplextype (' Person ', ' complexType ', ' struct ', ' all ', ',Array(       ' FirstName ' =Array(' name ' = ' FirstName ', ' type ' = ' xsd:string '),//The following type defines the types of data, this is a string' Age ' =Array(' name ' = ' age ', ' type ' = ' xsd:int '),//The following type defines the types of data, this is an int' Gender ' =Array(' name ' = ' gender ', ' type ' = ' xsd:string ')//The following type defines the types of data, this is a string     )      ); $server->wsdl->Addcomplextype (' Sweepstakesgreeting ', ' complexType ', ' struct ', ' all ', ',Array(       ' Greeting ' =Array(' name ' = ' greeting ', ' type ' = ' xsd:string '), ' winner ' =Array(' name ' = ' winner ', ' type ' = ' xsd:string ')       )      ); //server-defined soap invocation methods    $server->register (' Hello ',//method Name Hello, the method is below     Array(' person ' = ' tns:person '),//variables coming from the client     Array(' return ' = ' tns:sweepstakesgreeting '),//return Parameters' Urn:hellowsdl2 ',//Soap Name' Urn:hellowsdl2#hello ',//method Name of soap' RPC ',//How to use' Encoded ',//Coding' Test '//Archive    ); //defines a function that has been registered above Hello    functionHello$person) {       $greeting= ' Hello, '.$person[' FirstName ']. ‘. It's nice to meet a '.$person[' Age ']. ' Year old '.$person[' Gender ']. ‘.‘; $winner= ' Scott '; //the data to be returned     return Array(       ' Greeting ' =$greeting, ' winner ' =$winner       ); }      //Request (attempt) to invoke service    $HTTP _raw_post_data=isset($HTTP _raw_post_data) ?$HTTP _raw_post_data: ‘‘; $server->service ($HTTP _raw_post_data); ?>

B, the client

<?PHP//Package Letter Nusoap.phprequire_once('./lib/nusoap.php '); //Create a new SOAP client that invokes the WSDL//$client provided by the server = new SoapClient (' http://localhost/test/hellowsdl2.php?wsdl ', true); $client=NewSoapClient (' http://localhost/test/helloworld2.php '); //Check to see if it's an error$err=$client-GetError (); if($err) {   //Display Error Echo' $err. ' </pre> '; }  //parameters to be passed to the service side$person=Array(' FirstName ' = ' Willi ', ' age ' = ', ' gender ' = ' male '); //methods to invoke the service side$result=$client->call (' Hello ',Array(' person ' = =$person)); //Error Auditsif($client-fault) {   Echo' ; Print_r($result); Echo' </pre> '; } Else {   $err=$client-GetError (); if($err) {   Echo' $err. ' </pre> '; } Else {   Echo' ; Print_r($result); Echo' </pre> '; }  }  //Display Request InformationEcho' ; Echo' <pre> '.Htmlspecialchars($client->request, Ent_quotes). ' </pre> '; //Show return informationEcho' ; Echo' <pre> '.Htmlspecialchars($client->response, Ent_quotes). ' </pre> '; //Display Debug InformationEcho' ; Echo' <pre> '.Htmlspecialchars($client->debug_str, Ent_quotes). ' </pre> '; ?>

Above two examples whether the client, or the server side, are written in PHP, you can try to write in multiple languages, to test it. Whether you are using PHP module, or use Nusoap, the specific method is not here to say more, the handbook has.

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.