2011-08-26 2 views
3

PHP SoapVar 객체에 속성을 추가하려면 어떻게해야합니까? (PHP5, SoapClient, SoapVar)PHP SoapVar 객체에 속성을 추가하려면 어떻게해야합니까?

질문과 선택 사항이있는 섹션 ("답변")이 반복되는 SOAP 클라이언트 요청 상황이 있습니다. 그것은 모두 퀴즈 점수 산정 프로세스의 일부입니다. PHP5 Soap 클라이언트에서 본 것을 토대로, "answer"태그가 반복되기 때문에 어레이 접근 방식을 사용할 수 없습니다. 이것을 배열 구조로 쓰는 방법을 안다면 나를 고쳐주세요.

그래서 SoapVar 객체를 사용하여 복잡한 객체 구조를 정의했습니다. 나가 외부 꼬리표의 한에있는 속성을 필요로 할 때까지 모두는 상당히 잘 갔다; identityRequest.

<ns1:invokeIdentityService> 
<ns1:identityRequest> 
<ns1:scoreRequest> 
<ns1:quizId>6982971</ns1:quizId> 
<ns1:answer> 
<ns1:questionId>25867508</ns1:questionId> 
<ns1:choiceId>128423504</ns1:choiceId> 
</ns1:answer> 
<ns1:answer> 
<ns1:questionId>25867509</ns1:questionId> 
<ns1:choiceId>128423507</ns1:choiceId> 
</ns1:answer> 
<ns1:answer> 
<ns1:questionId>25867510</ns1:questionId> 
<ns1:choiceId>128423514</ns1:choiceId> 
</ns1:answer> 
</ns1:scoreRequest> 
</ns1:identityRequest> 

배열 구조를 사용하여 특성을 추가하는 방법을 살펴 보았지만 복잡한 개체로 어떻게 수행 할 수 있습니까? 나는 PHP 책이나 온라인 어디서나 대답 할 수 없다.

SOAP 본문에 표시 될 identityRequest 객체에 4 개의 속성 (customerReference, locale, productAlias ​​및 transactionId)을 추가해야합니다.

$answers = array(); 
// Start array with Quiz ID: quizId and questionId/choiceId are at the same level. 
$answers[] = new SoapVar($quiz_id, XSD_STRING, null, null, 'quizId', $this->is); 

foreach ($input as $name=>$value) { 
    if (preg_match('/^question([0-9]*)$/i', $name, $parts)) { 
     // Build questionId/choiceId 
     $answer = array(); 
     $answer[] = new SoapVar($parts[1], XSD_STRING, null, null, 'questionId', $this->is); 
     $answer[] = new SoapVar($value , XSD_STRING, null, null, 'choiceId', $this->is); 
     // Build answer 
     $answers[] = new SoapVar((array)$answer, SOAP_ENC_OBJECT, null, null, 'answer', $this->is); 
    } 
} 

// Build scoreRequest from $answers (includes quizId) 
$scoreRequest = new SoapVar((array)$answers, SOAP_ENC_OBJECT, null, null, 'scoreRequest', $this->is); 

// Wrap with identityRequest 
$identityRequest = new SoapVar(array('scoreRequest' => $scoreRequest), SOAP_ENC_OBJECT, null, null, 'identityRequest', $this->is); 

$params = array(
    'identityRequest' => $identityRequest, 
    'customerReference' => 'Company12345', 
    'locale' => 'en_US', 
    'productAlias' => 'Alias6789', 
    'transactionID' => 'transId1234', 
); 
$request = new SoapVar($params, SOAP_ENC_OBJECT, null, null, 'request', $this->is); 

어떻게 4 가지 속성을 SoapVar에 추가 할 수 있습니까? 위에서 볼 수 있듯이 $ params와 object/array의 조합을 시도했지만 작동하지 않습니다. 난 그냥 몇 가지 추가적인 XML 태그/값으로 끝납니다. 나는 누군가가 도움을 제공 할 수 있기를 바랍니다

<ns1:invokeIdentityService ns1:customerReference="Company12345" ns1:locale="en_US" ns1:productAlias="Alias6789" ns1:transactionID="transId1234"> 

:

내가 좋아하는 뭔가를 찾고 있어요. 난 지쳤어. PHP5 SOAP로 인해 많은 어려움을 겪었습니다. 나는 항상 그것이 필요로하는 것보다 더 힘들 것 같다. 고맙습니다. Jeff

+0

왜 배열을 사용하지 않으시겠습니까? 복잡한 객체를 만드는 방법은 http://www.php.net/manual/en/book.soap.php#83409를 참조하십시오. –

+0

내가 말할 수있는 한, "대답"과 같은 반복 요소가있는 배열을 사용할 수 없습니다. 아니면 배열 스타일을 반복하는 방법이 있습니까? – jjwdesign

+0

stdClass() 개체를 사용하여 특성을 성공적으로 추가했습니다. 요청은 SoapVar 객체와 stdClass 객체의 조합이었습니다. 일하는 것 같습니다. BTW, 비누 요청에 개체와 배열을 혼합하려고하면 작동하지 않습니다. – jjwdesign

답변

0

누군가 stdClass() 객체에 대해 좀 더 자세한 정보를 원했기 때문에 여기 있습니다. 나는 이것이이 문제를 코드화하는 좋은 방법이라고 주장하지 않는다. 더 나은 방법이 될 수 밖에 없습니다.

// Build scoreRequest from $answers (includes quizId) 
    $scoreRequest = new SoapVar((array)$answers, SOAP_ENC_OBJECT, null, null, 'scoreRequest', $this->is); 

    // SoapVar not used pass this point; see below. 
    //$identityRequest = new SoapVar(array('scoreRequest' => $scoreRequest), SOAP_ENC_OBJECT, null, null, 'identityRequest'); 

    // scoreQuizRequest uses a mixture of SoapVar Objects and stdClass Objects. 
    // stdClass Object is used to allow us to define the attributes for 
    // identityRequest, such as productAlias & transactionID. 
    // Note: $scoreRequest is a SoapVar Object. 
    $request = new stdClass(); 
    $request->identityRequest = new stdClass(); 
    $request->identityRequest->scoreRequest = $scoreRequest; 

    $request->identityRequest->customerReference = $this->customerReference; 
    $request->identityRequest->locale = $this->locale; 
    $request->identityRequest->productAlias = $this->productAlias; 
    $request->identityRequest->transactionID = $this->transactionId; 
+0

나를 위해 작동하지 않습니다 : - / – geoidesic

관련 문제