2014-11-04 67 views
0

我试图设置一个简单的Web服务,但我遇到了麻烦。该服务似乎可用,但我似乎无法返回响应。 $ client上的Var_dump显示了一个到Web服务的连接,但没有任何回应。也没有发现错误。使用PHP NuSOAP创建Web服务时遇到困难

任何帮助将不胜感激。

<?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; 
} 
?> 
+0

为什么不使用带有内置SoapClient和SoapServer的PHP Soap扩展?它更快,更少bug,更多功能和大量示例。 – lubosdz 2014-11-04 23:13:00

+0

我试着根据你的建议,但没有成功。我结束了切换到JSON。我创建的应用程序是相当基本的,客户端将用.net编写。我已经阅读过.net客户端通过SOAP与PHP服务器进行通信时遇到困难,所以这可能是更好的方法。 – Liam 2014-11-06 18:22:16

回答

0

有在客户端和服务器文件的代码中的bug,我已经更新了代码

server.php

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 
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(); 
?> 

客户端.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; 
} 
?> 

它的工作!