PHP calls Webservice implementation code

Source: Internet
Author: User

If you need to use PHP to call Webservice, We must confirm that your php configuration file has opened the soap extension, that is, extension = php_soap.dll; otherwise, you cannot use PHP to call Webservice, for more information, see.

OK. Now let's try 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 the ip address of the your SERVER.
$ 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 soap. php
Try {
$ Client = new SoapClient (null,
Array ('location' => "http: // 192.168.1.179/test/serverSoap. php", 'url' => "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 function. Let's create another class.

// Server-side serverSoap. php
$ ClassExample = array ();

$ Soap = new SoapServer (null, array ('url' => "http: // 192.168.1.179/", 'classexample '=> $ classExample ));
$ Soap-> setClass ('chesterclass ');
$ Soap-> handle ();

Class chesterClass {
Public $ name = 'chester ';

Function getName (){
Return $ this-> name;
}
}

// Client soap. php

Try {
$ Client = new SoapClient (null,
Array ('location' => "http: // 192.168.1.179/test/serverSoap. php", 'url' => "http: // 127.0.0.1 /")
);
Echo $ client-> getName ();

} Catch (SoapFault $ fault ){
Echo "Error:", $ fault-> faultcode, ", string:", $ fault-> faultstring;
}


I have passed the test of the above Code.


Proxy call

The Code is as follows: Copy code

<?
/*************************************** ***************************************/
/* File name: soapclient. php
/* Description: WebService interface client routine
/*************************************** ***************************************/
Require ('nusoap. php ');

// Create a soapclient object whose parameter is server's WSDL
$ Client = new soapclient ('HTTP: // localhost/Webservices/Service. asmx? WSDL ', 'wsdl ');

// Generate proxy class
$ Proxy = $ client-> getProxy ();

// Call a remote Function
$ 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 call. NET WebService or J2EE WebService may have encountered Chinese garbled characters. The following describes the causes and solutions of this problem.

Reasons for garbled WebService calling by NuSoap:

Usually we do WebService development is used for UTF-8 encoding, then we need to set:

The Code is as follows: Copy code


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

At the same time, xml must be transmitted in the same encoding mode:

The Code is as follows: Copy code

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

At this point, everything is normal, but when we output the results, we find that the returned code is garbled.

Solution to WebService call garbled by NuSoap:

In fact, a friend who has enabled the debugging function will find that $ client-> response returns the correct result. Why $ result = $ client-> call ($ action, array ('parameters '=> $ param); is it garbled?

After studying the NuSoap code, we will find that when xml_encoding is set to UTF-8, NuSoap will detect the decode_utf8 setting. If it is true, it will execute the utf8_decode function in PHP, nuSoap is set to true by default. Therefore, 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 ';

Related Article

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.