PHP calls WebService two implementation code

Source: Internet
Author: User

OK now we're going to experience webservice

The code is as follows Copy Code

Server-Side serversoap.php

$soap = new SoapServer (Null,array (' uri ' => ' http://192.168.1.179/');//this URI is your SERVER IP.
$soap->addfunction (' Minus_func '); Register the function
$soap->addfunction (Soap_functions_all);
$soap->handle ();

function Minus_func ($i, $j) {
$res = $i-$j;
return $res;
}

Client Side clientsoap.php
try {
$client = new SoapClient (NULL,
Array (' Location ' => "http://192.168.1.179/test/serverSoap.php", ' uri ' => ' http://127.0.0.1/')
);
echo $client->minus_func (100,99);

catch (SoapFault $fault) {
echo "Error:", $fault->faultcode, ", String:", $fault->faultstring;
}

This is an example of a client calling a server-side function, and we'll get another class.

Server-Side serversoap.php
$classExample = Array ();

$soap = new SoapServer (Null,array (' uri ' => ' http://192.168.1.179/', ' classexample ' => $classExample));
$soap->setclass (' Chesterclass ');
$soap->handle ();

Class Chesterclass {
Public $name = ' Chester ';

function GetName () {
return $this->name;
}
}

Client Side clientsoap.php

try {
$client = new SoapClient (NULL,
Array (' Location ' => "http://192.168.1.179/test/serverSoap.php", ' uri ' => ' http://127.0.0.1/')
);
echo $client->getname ();

catch (SoapFault $fault) {
echo "Error:", $fault->faultcode, ", String:", $fault->faultstring;
}


The above code I have tested through


Proxy method call

The code is as follows Copy Code

?
/******************************************************************************/
/* FileName: soapclient.php
/* Description: WebService interface Client Routines
/******************************************************************************/
Require (' nusoap.php ');

Creates a SoapClient object, which is the WSDL of the server
$client =new soapclient (' Http://localhost/Webservices/Service.asmx?WSDL ', ' WSDL ');

Generate proxy class
$proxy = $client->getproxy ();

Calling remote functions
$aryResult = $proxy->login (' username ', MD5 (' password '));

Echo $client->debug_str;
/*
if (! $err = $proxy->geterror ()) {
Print_r ($aryResult);
} else {
Print "ERROR: $err";
}
*/

$document = $proxy->document;
Echo <<<soapdocument
<?xml version= "1.0" encoding= "GB2312"?>
<soap-env:envelope soap-env:encodingstyle= "http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap-env= "http:// schemas.xmlsoap.org/soap/envelope/"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:si=" http:// Soapinterop.org/xsd ">
<SOAP-ENV:Body>
$document
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Soapdocument;

?>

Many friends who use Nusoap to invoke. NET WebService or Java EE WebService may have encountered Chinese garbled problems, the following describes the causes of the problem and the corresponding solutions.

Nusoap Call WebService the reason for garbled:

Usually we use the UTF-8 code for WebService development, when we need to set:

The code is as follows Copy Code


$client->soap_defencoding = ' utf-8 ';

At the same time, XML needs to be passed in the same encoding:

The code is as follows Copy Code

$client->xml_encoding = ' utf-8 ';

At this point should be all normal, but we are in the output of the results, but found that the return of garbled.

Nusoap Call WebService a garbled solution:

In fact, a friend who has turned on the debugging feature will find that $client->response is returning the correct result, why $result = $client->call ($action, Array (' Parameters ' => $ param)); But it's garbled?

After studying the NUSOAP code, we find that when xml_encoding is set to UTF-8, Nusoap detects Decode_utf8 settings, and if true, executes the Utf8_decode function in PHP, and Nusoap defaults to True , so we need to set:

The code is as follows Copy Code

$client->soap_defencoding = ' utf-8 ';
$client->decode_utf8 = false;
$client->xml_encoding = ' utf-8 ';

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.