繁体   English   中英

如何向 PHP SoapVar 对象添加属性?

[英]How can I add Attributes to a PHP SoapVar Object?

如何向 PHP SoapVar 对象添加属性? (PHP5, SoapClient, SoapVar)

我有一个 SOAP 客户端请求情况,我有重复的部分(“答案”),每个部分都有一个问题和选择。 这都是测验评分过程的一部分。 根据我在 PHP5 SOAP 客户端上看到的情况,数组方法不可行,因为“答案”标签是重复的。 如果你知道如何用数组结构写这个,请纠正我。

因此,我使用 SoapVar 对象来定义复杂的对象结构。 一切都很顺利,直到我注意到我需要在一个外部标签中使用 Attributes; 身份请求。

<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 混合使用,但这似乎不起作用。 我最终得到了几个额外的 XML 标签/值。

我正在寻找类似的东西:

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

我希望有人可以提供一些帮助。 我累极了。 我一直在努力使用 PHP5 SOAP,就像许多其他人一样。 我似乎总是比它需要的更难。

有人想要更多关于 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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM