SOAP WebService Interface

Source: Internet
Author: User
Tags php server
In PHP, after the php_soap.dll extension is turned on in the php.ini file, soap can be supported.
In the SOAP extension library, there are three main objects.
1, SoapServer
Defines the functions that can be called and returns the response data when creating a PHP server-side page. The syntax format for creating a SoapServer object is as follows:
$soap = new SoapServer ($wsdl, $array);
Where the WSDL file is used $wsdl for SHOAP, WSDL is a standard format for describing Web service, and if $WSDL is set to NULL, the WSDL pattern is not used. $array is the property information for SoapServer, which is an array.
The AddFunction method of the SoapServer object is used to declare which function can be called by the client, with the following syntax:
$soap->addfunction ($function _name);
Where $soap is a SoapServer object, $function _name is the name of the function that needs to be called.
The handle method of the SoapServer object is used to process the user input and invoke the corresponding function, and finally returns the result to the client for processing. The syntax format is as follows:
$soap->handle ([$soap _request]);
Where $soap is an SoapServer object, $soap _request is an optional parameter that represents the user's request information. If you do not specify $soap_request, it means that the server will receive all requests from the user.
2, Soapcliet
Used to invoke the SoapServer page on the remote server and implement a call to the corresponding function. The syntax format for creating a SoapClient object is as follows:
$soap = new SoapClient ($wsdl, $array);
where parameters $wsdl and $array are the same as soapserver.
After creating the SoapClient object, invoking a function in the service-side page is equivalent to calling the SoapClient method, creating the following syntax:
$soap->user_function ($params);
Where $soap is a SoapClient object, User_function is the function to be called on the server side, $params is the parameter to pass in the function.
3, SoapFault
SoapFault is used to generate errors that may occur during SOAP access. The syntax format for creating a SoapFault object is as follows:
$fault = new SoapFault ($faultcode, $faultstring);
Where $faultcode is a user-defined error code, $faultstring is a user-defined error message. The SoapFault object is generated automatically when an error occurs on the server-side page, or when the user creates the SoapFault object themselves. For errors that occur with SOAP access, the client can obtain the appropriate error message by capturing the Soapfalut object.
After the client captures the SoapFault object, you can obtain the error code and error message by using the following code:
$fault->faultcode;//Error code
$fault->faultstring;//Error message
Where $fault is the SoapFault object that you created earlier.
Both SoapServer and soapclient receive two parameters, the second of which is option, which supports several options, here we use:
URI: namespace, client and server need to use the same namespace
Location: The client used to specify the access address of the server-side program, which is the program address of the second code in this example.
Trace: The client, when true, can obtain the contents of the server and client communication for debugging purposes.

soapserver.php

Java code

<?php

Create an instance of the SoapServer object first, then register the function we want to expose,

The last handle () is used to process the accepted SOAP request

Error_reporting (7); When officially released, set to 0

Date_default_timezone_set (' PRC '); Setting the time zone

/* Several functions for client-side invocation */

function reverse ($STR)

{

$retval = ";

if (strlen ($STR) < 1) {

return new SoapFault (' Client ', ' ', ' Invalid string ');

}

for ($i = 1; $i <= strlen ($STR); $i + +) {

$retval. = $str [(strlen ($STR)-$i)];

}

return $retval;

}

function Add2numbers ($num 1, $num 2)

{

if (Trim ($num 1)! = Intval ($num 1)) {

return new SoapFault (' Client ', ' ', ' the first number is invalid ');

}

if (Trim ($num 2)! = Intval ($num 2)) {

return new SoapFault (' Client ', ' ', ' the second number is invalid ');

}

Return ($num 1 + $num 2);

}

function gettime ()

{

$time = Date (' y-m-d h:i:s ', Time ());

return $time;

}

$soap = new SoapServer (null, array (' uri ' = "Httr://test-rui"));

$soap->addfunction (' reverse ');

$soap->addfunction (' add2numbers ');

$soap->addfunction (' gettime ');

$soap->addfunction (Soap_functions_all);

$soap->handle ();

?>

soapclient.php

Java code

<?php

Error_reporting (7);

try {

$client = new SoapClient (null, Array (' location ' = ' = ' http://www.yiigo.com/Soapserver.php ', ' uri ' = = '/') '/' Test-uri "));

$str = "This string would be reversed";

$reversed = $client->reverse ($STR);

echo "If you reverse ' $str ' and you'll get ' $reversed '";

$n 1 = 20;

$n 2 = 33;

$sum = $client->add2numbers ($n 1, $n 2);

echo "<br>";

echo "If you try $n 1 + $n 2 and you'll get $sum";

echo "<br>";

echo "The Remoye system time is:". $client->gettime ();

} catch (SoapFault $fault) {

echo "fault! Code: ". $fault->faultcode. "String:". $fault->faultstring;

}

?>

If you reverse ' this string would be reversed ', you'll get ' desrever eb lliw gnirts siht '
If you try + +, you'll get 53
The Remoye system time is:2012-05-28 16:14:29

Identity authentication via SoapHeader

Java code

<?php

Class Server

{

Public Function auth ($a)

{

if ($a! = ' 123456789 ') {

throw new SoapFault (' Server ', ' User authentication information error ');

}

}

Public function Say ()

{

Return ' Hi ';

}

}

$srv = new SoapServer (null, array (' uri ' = ' http://localhost/namespace '));

$srv->setclass (' Server ');

$srv->handle ();

Client

Java code

<?php

$CLI = new SoapClient (NULL,

Array (' uri ' = ' http://localhost/namespace/',

' Location ' = ' http://localhost/server.php ',

' Trace ' = true);

Auth function to be processed by the server 12345689 is a parameter

$h = new SoapHeader (' http://localhost/namespace/',

' Auth ', ' 123456789 ', false, Soap_actor_next);

$CLI->__setsoapheaders (Array ($h));

try {

echo $cli->say ();

} catch (Exception $e) {

echo $e->getmessage ();

}

Note that the server class in server.php has a method "auth", just corresponding to the name of the header, method auth parameter $u, that is SoapHeader Data,soapserver receive this request, first call Auth method, and pass "123456789" as a parameter to the method. When the MustUnderstand parameter is False, the Say method is called even if there is no auth this method, but if it is true, if the Auth method does not exist, it returns a SoapFault informing the header that it is not being processed. The actor parameter named those role must handle the header, here I understand not too thorough, not to say.

Java code

$file = $this->getsoapwsdl ();

$client = new SoapClient ($file),//url can be accessed through the browser and cannot be directly invoked to resolve

$param = Array (' UserID ' = ' test ', ' merchantid ' = ' test ');

$returnSt = $client->checkuser ($param);

Print_r ($returnSt->checkuserresult);

Public Function getsoapwsdl ()

{//Periodically save the URL file to a local

$file = Mage::getbasedir (). Ds. ' Data '. Ds. ' Shengda '. Ds. ' EXPORT.WSDL ';

if (Time () > Filemtime ($file) + 7 * 86400) {

$url = "Http://jf.sdo.com/ExchangeScore/ExchangeService.asmx?WSDL";

Include_once (BP). Ds. "lib/snoopy.class.php");

$snoopy = new Snoopy;

$snoopy->fetch ($url); Get all content

$snoopy->read_timeout = 4;

$WSDL = $snoopy->results;

if ($snoopy->status = = ' &&! $snoopy->timed_out) {

if (!is_dir (DirName ($file))) {

mkdir (DirName ($file));

}

File_put_contents ($file, $wsdl);

}

}

return $file;

}

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