2014-11-04 80 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; 
} 
?> 

它的工作!