In PHP, Nusoap calls the Java Axis2 published WebService

Source: Internet
Author: User
Tags wsdl

Call WebService

The development environment is: Eclipse3.6 (Java IDE) + JDK 1.6 + Tomcat 7.0 + Axis2 1.6 + php 5.2.14 (IDE eclipse-php-helios-sr1-win32.zip) + apache2 .2.15, these software are updated at any time, please readers Google Baidu to download the website.

First, write services in the Java side, and use Axis2 to publish into services

The service name for this article is Sayhi, which has a function sayhello ():

The code is as follows Copy Code
Package Hi;
Publicclass Sayhi {
public string SayHello (string user,string info) {
Return "Hello, my Friend" + user + info;
}
}

Second, write PHP client, call Axis2 released WebService

2.1 Download Nusoap.rar, add the extracted Lib folder to the project

2.2 Writing client PHP, the contents are as follows:

The code is as follows Copy Code

<?php
Require_once ("./lib/nusoap.php");

The WebService path to access
$NusoapWSDL = "HTTP://LOCALHOST:8080/AXIS2/SERVICES/SAYHI?WSDL";

Generating Client Objects
$client = new Nusoap_client ($NusoapWSDL);
$client->soap_defencoding = ' UTF-8 ';
$client->decode_utf8 = false;

Set parameters (Note: PHP can only pass parameters in ' array set ' mode)
$param = Array (' user ' => ' country fake travel ',
' Info ' => ' Welcome to my home! ');

Call remote method ' SayHello ' as a method name in the service
' Http://Hi ' is a targetnamespace value in the WSDL file
$result = $client->call (' SayHello ', $param, ' http://Hi ');

Show execution Results
if (! $err = $client->geterror ()) {
echo $result;
}else{
Echo $err;
}
?>

Third, the results of implementation are:

Hello, my Friend country fake not travel Welcome to my home!

WebService garbled problem

Many friends who use Nusoap to invoke. NET WebService or Java EE WebService may have encountered a Chinese garbled problem (I used PHP to invoke Nusoap), and the following describes the causes of this 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:

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

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

$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:

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.