php - 如何将属性添加到 PHP SoapVar 对象?

标签 php soap

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

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

因此,我使用 SoapVar 对象来定义复杂的对象结构。一切都进行得相当顺利,直到我注意到我需要在一个外部标签中添加属性;身份请求。

<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;

关于php - 如何将属性添加到 PHP SoapVar 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7211022/

相关文章:

javascript - 如何在php foreach循环中获取id和类型并通过ajax发送到服务器?

php - 如何在 PHP 和 MySQL 上使用循环?

java - 将 JAXWS 应用程序从 Java 8 迁移到 Java 11

可部署在 Tomcat、JBoss、Geronimo 等中的 Java SOAP Server 等等

c# - 为什么我会得到这个 XML block 的 org.xml.sax.SAXException?

java - 在 apache karaf 上使用 apache Camel 蓝图的 Soap 请求失败

php - 如何减少mysql、codeigniter数据的加载时间

php - 确定用户是否正在使用代理

php - CodeIgniter 模型无法返回特定记录

soap - Go 上的 WSDL/SOAP 支持?