2013-01-01 4 views
1

tl; dr : SOAP 요청에 대한 응답으로 다중 문자열 보내기.PHP에서 SOAP를 통해 여러 값 반환하기

저는 SOAP을 처음 사용합니다. SOAP를 통해 요청을 처리하는 간단한 웹 서비스를 작성했습니다. PHP에서 이것을 구현하고자하므로 NuSOAP 라이브러리를 사용했습니다. SOAP API 디자인을 위해 나에게 주어진 사양은 다음과 같다.

요청 형식 :

<?xml version="1.0" encoding="utf-8"?>  
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://www.sandeepraju.in/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
<soapenv:Body>  
<q0:getData>  
<token>String</token>  
</q0:getData>  
</soapenv:Body>  
</soapenv:Envelope> 

예/샘플 응답은 다음과 같이

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> 
<S:Body> 
<ns2:getDataResponse xmlns:ns2="http://www.sandeepraju.in/"> 
<return> 
<Data> 
    <animal>cat</animal> 
    <phone>225</phone> 
    <code>FYI</code> 
</Data> 

나는 위의 사양에 대해 쓴 PHP 코드입니다. 여기

require_once("../nusoap_old/lib/nusoap.php"); 

// Definition of getData operation 
function getData($token) { 
    if($token == "somestring") { 
     return array("animal" => "cat", "phone" => "225", "code" => "FYI"); 
    } 
    else { 
     return array("animal" => "null", "phone" => "null", "code" => "null"); 
    } 
} 

// Creating SOAP server Object 
$server = new soap_server(); 

// Setup WSDL 
$server->configureWSDL('catnews', 'urn:catinfo'); 

$server->wsdl->addComplexType('return_array_php', 
    'complexType', 
    'struct', 
    'all', 
    '', 
    array(
    'animal' => array('animal' => 'animal', 'type' => 'xsd:string'), 
    'phone' => array('phone' => 'phone', 'type' => 'xsd:string'), 
    'code' => array('code' => 'code', 'type' => 'xsd:string') 
    ) 
); 

// Register the getData operation 
$server->register("getData", 
    array('token' => 'xsd:string'), 
    array('return' => 'tns:return_array_php'), 
    'urn:catinfo', 
    'urn:catinfo#getData'); 

// Checking POST request headers 
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA)? $HTTP_RAW_POST_DATA : ""; 
$server->service($HTTP_RAW_POST_DATA); 

, 나는 내가 하지하는 PHP 배열을 반환한다고 생각합니다. 하지만 사양에 따라 무엇을 반환해야하는지 잘 모르겠습니다. 누구든지 이걸 도와 줄 수 있어요. 또는 배열을 반환 올바른지?

+0

이것은 Nusoap이 어떻게 처리하는지에 달려 있다고 말하고 싶습니다. 나는 특별히 Nusoap을 모른다. 그래서 나는 그것과 별개로 많은 도움을 줄 수 없다. 어쩌면 그들의 문서에 설명되어 있을까요? – hakre

+0

네가 맞습니다. NuSoup에 대한 많은 문서를 찾지 못했습니다. 나는 해결책이 있다고 생각한다. 나는 그것을 시험해야한다. 나는 그것이 정확하다, 나는 내일 여기에서 그것을 수정할 것이다. 그래서 다른 사람들도 똑같은 문제에 직면한다면 그것을 사용할 수있다. :) –

+0

솔루션을 찾은 경우 (절반 정도만) 해결책으로 남겨 둡니다. 당신도 그것을 받아 들일 수 있습니다. 이것은이 웹 사이트에서 완전히 유효하며 매우 환영합니다. 미안해, 나는 더 이상 도움이 안돼. – hakre

답변

3

데이터로 구성된 배열에 대해 다른 복합 유형을 추가해야합니다. 이처럼 :

$server->wsdl->addComplexType(
    'dataArray', // MySoapObjectArray 
    'complexType', 'array', '', 'SOAP-ENC:Array', 
    array(), 
    array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:return_array_php[]')), 'tns:return_array_php' 
); 

은 함수의 리턴 값으로 새로운 데이터 유형을 등록합니다.

$server->register(
    'getData',  
    array('Datum'=>'xsd:token'), 
    array('return'=>'tns:dataArray'), 
    $namespace, 
    false, 
    'rpc', 
    'encoded', 
    'description' 
); 

그러면 함수가 배열의 단일 부분을 설정해야합니다.

function GetData($token) 
{ 
    if($token == "somestring") { 
     $result[0] = array(
      "animal" => "cat", 
      "phone" => "225", 
      "code" => "FYI" 
     ); 

     $result[1] = array(
      "animal" => "dog", 
      "phone" => "552", 
      "code" => "IFY" 
     ); 
    } else { 
     $result = null; 
    } 
    return $result; 
} 

문자열 "somestring"로 불리는이 서비스의 응답 될 것입니다 : 귀하의 사양과 일치

<ns1:getDataResponse xmlns:ns1="http://localhost/status/status.php"> 
    <return xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:return_array_php[2]"> 
     <item xsi:type="tns:return_array_php"> 
      <animal xsi:type="xsd:string">cat</animal> 
      <phone xsi:type="xsd:string">225</phone> 
      <code xsi:type="xsd:string">FYI</code> 
     </item> 
     <item xsi:type="tns:return_array_php"> 
      <animal xsi:type="xsd:string">dog</animal> 
      <phone xsi:type="xsd:string">552</phone> 
      <code xsi:type="xsd:string">IFY</code> 
     </item> 
    </return> 
</ns1:getDataResponse> 

합니다.

관련 문제