简体   繁体   中英

Trouble creating web service with PHP NuSOAP

I'm trying to set up a simple web service but I'm having trouble. The service seems to be available, but I can't seem to return a response. Var_dump on $client shows a connection to the web service, but nothing comes back in response. No errors are caught either.

Any help would be greatly appreciated.

server.php

<?php
require_once ("lib/nusoap.php");
$URL = "https://www.domain.com";
$namespace = $URL . '?wsdl';

$server = new soap_server;
$server->debug_flag = false;
$server->configureWSDL('Test', $namespace);
$server->wsdl->schemaTargetNamespace = $namespace;

function get_message($your_name)
{
    if(!$your_name)
    {
        return new soap_fault('Client','','Put Your Name!');
    }

    $result = "Welcome to ".$your_name .". Thanks for Your First Web Service Using PHP with SOAP";
    return $result;
}

$server->register('get_message');

// create HTTP listener

$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
exit();
?>

client.php

<?php
$wsdl = "https://www.domain.com/webservice/server.php?wsdl";

require_once ('lib/nusoap.php');

$param = array("your_name" => "Liam");

$client = new SoapClient($wsdl, array("trace" => true));

$response = $client->get_message($param);

if($client->fault)
{
    echo "FAULT: <p>Code: (".$client->faultcode."</p>";
    echo "String: ".$client->faultstring;
}
else
{
    echo $response;
}
?>

There were bugs in the code of client and server files, I have updated the code

server.php

<?php
require_once ("lib/nusoap.php");
$URL = "http://www.domian.com/server.php";
$namespace = $URL . '?wsdl';

$server = new soap_server(); //added ()
$server->debug_flag = false;
$server->configureWSDL('Test', $namespace);
//$server->wsdl->schemaTargetNamespace = $namespace;

function get_message($your_name)
{
    if(!$your_name)
    {
        return new soap_fault('Client','','Put Your Name!');
    }

    $result = "Welcome to ".$your_name .". Thanks for Your First Web Service Using PHP with SOAP";
     return $result;
 }

$server->register('get_message',
                    array('request' => 'xsd:ArrayReq'), // ArrayReq is type for your parameters
                    array('return' => 'xsd:String'),
                    'urn:Test',
                    'urn:Test#get_message',
                    'rpc',
                    'encoded',
                    'show message'); // added request return array

// create HTTP listener

//$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service(file_get_contents('php://input'));
exit();
?>

Client.php

<?php
$wsdl = "http://www.domian.com/server.php?wsdl";

require_once ('lib/nusoap.php');

$param = array("your_name" => "Liam");
$client = new nusoap_client($wsdl); // user nusoap_client() for creating client object
//$client = new SoapClient($wsdl, array("trace" => true));

$response = $client->call('get_message', $param); // use call() to call the server functions

if($client->fault)
{
   echo "FAULT: <p>Code: (".$client->faultcode."</p>";
   echo "String: ".$client->faultstring;
}
else
{
  echo $response;
}
?>

it's working!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM