【问题标题】:Displaying SOAP API results in PHP在 PHP 中显示 SOAP API 结果
【发布时间】:2018-11-01 10:36:45
【问题描述】:

我很难找到这方面的资源,使用 PHP 连接到 SOAP API 并轻松地将数组结果附加到变量以进行显示。

这是 API 中 GetCostCenters 函数的 SOAP API XML:

<Response><Status><Result>1</Result><Description>OK</Description></Status><CostCenters><CostCenter><ID>4</ID><Branch>Adelaide</Branch></CostCenter><CostCenter><ID>6</ID><Branch>Derby</Branch></CostCenter><CostCenter><ID>33</ID><Branch>GT Perth</Branch></CostCenter><CostCenter><ID>7</ID><Branch>Hobart Branch</Branch></CostCenter><CostCenter><ID>46</ID><Branch>Lawns & Maintenance</Branch></CostCenter><CostCenter><ID>10</ID><Branch>London</Branch></CostCenter><CostCenter><ID>3</ID><Branch>Melbourne</Branch></CostCenter><CostCenter><ID>45</ID><Branch>NECA Apprentices</Branch></CostCenter><CostCenter><ID>17</ID><Branch>New York</Branch></CostCenter><CostCenter><ID>48</ID><Branch>Nursing NSW</Branch></CostCenter><CostCenter><ID>1</ID><Branch>Perth</Branch></CostCenter><CostCenter><ID>44</ID><Branch>Registration</Branch></CostCenter><CostCenter><ID>16</ID><Branch>Rio</Branch></CostCenter><CostCenter><ID>50</ID><Branch>Subiaco</Branch></CostCenter><CostCenter><ID>51</ID><Branch>Subiaco</Branch></CostCenter><CostCenter><ID>2</ID><Branch>Sydney</Branch></CostCenter><CostCenter><ID>42</ID><Branch>test - tester</Branch></CostCenter><CostCenter><ID>49</ID><Branch>TesterOM</Branch></CostCenter><CostCenter><ID>8</ID><Branch>Tom Price</Branch></CostCenter><CostCenter><ID>47</ID><Branch>Traffic Control NSW</Branch></CostCenter></CostCenters></Response>

这是我的 PHP:

    $client = new SoapClient("https://api.myfeed.com/feed.asmx?WSDL", array(
    'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
    'trace'    => true
 ));

// GET RESULTS
    $result = $client->GetCostCenters(array(
        'CompanyID'     => 'MYID',
        'APIKey'     => 'MYKEY',
        'APIPassword' => 'MYPASS',
        'features'  =>  SOAP_SINGLE_ELEMENT_ARRAYS,
        'trace' =>  true
    ));

    echo '<pre>';
    print_r($result);
    echo '</pre>';

像这样在 [any] 中的一行上输出:

stdClass Object
(
    [GetCostCentersResult] => stdClass Object
        (
            [any] => 1OK4Adelaide6Derby33GT Perth7Hobart Branch46Lawns & Maintenance10London3Melbourne45NECA Apprentices17New York48Nursing NSW1Perth44Registration16Rio50Subiaco51Subiaco2Sydney42test - tester49TesterOM8Tom Price47Traffic Control NSW
        )

)

我不知道为什么,理想情况下我希望它作为一个数组,这样我就可以分配变量并运行一个循环来显示信息。

有什么想法吗?提前致谢。

【问题讨论】:

  • 可以将 stdClass 转换为数组:$result = (array)$result;
  • @DanielO - 谢谢,但我不确定我是否理解,我该如何将其应用于上述内容?

标签: php soap soap-client


【解决方案1】:

将结果从字符串转换为xml对象(simplexml_load_string):

$resultAsXml = simplexml_load_string($result);

然后使用json编码(json_encodejson_decode)将对象转换为数组:

$resultAsArray = json_decode(json_encode($resultAsXml), true);

注意传递给 json_decode 的第二个参数:

true时,JSON对象会作为关联数组返回;

在你的情况下:

$client = new SoapClient("https://api.myfeed.com/feed.asmx?WSDL", array(
    'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
    'trace'    => true
));

// GET RESULTS
$result = $client->GetCostCenters(array(
    'CompanyID'     => 'MYID',
    'APIKey'     => 'MYKEY',
    'APIPassword' => 'MYPASS',
    'features'  =>  SOAP_SINGLE_ELEMENT_ARRAYS,
    'trace' =>  true
));

$resultAsXml = simplexml_load_string($result);
$resultAsArray = json_decode(json_encode($resultAsXml), true);

echo '<pre>';
print_r($resultAsArray);
echo '</pre>';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    相关资源
    最近更新 更多