2011-08-26 42 views
3

如何將屬性添加到PHP SoapVar對象? (PHP5,SoapClient,SoapVar)如何將屬性添加到PHP SoapVar對象?

我有一個SOAP客戶端請求情況,我有重複部分(「答案」),每個部分都有一個問題和選擇。這是測驗評分過程的一部分。基於我在PHP5 Soap Client中看到的內容,數組方法不可行,因爲「答案」標記是重複的。如果你知道如何用數組結構寫這個,請糾正我。

所以,我用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書籍或在線任何地方回答這個問題。

我需要將4個屬性(customerReference,locale,productAlias和transactionId)添加到將顯示在SOAP主體中的identityRequest對象。

$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的對象/數組的混合,但這似乎不起作用。我只是結束了幾個額外的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對象的組合。似乎工作。順便說一句,試圖混合對象和數組到肥皂請求不起作用。 – 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