显示 SOAP API 结果 PHP
Displaying SOAP API results in PHP
我很难找到这方面的资源,使用 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
)
)
我不明白为什么,理想情况下我希望它是一个数组,这样我就可以分配变量和 运行 一个循环来显示信息。
有什么想法吗?提前致谢。
将结果从字符串转换为 xml 对象 (simplexml_load_string):
$resultAsXml = simplexml_load_string($result);
然后使用json编码(json_encode, json_decode)将对象转换为数组:
$resultAsArray = json_decode(json_encode($resultAsXml), true);
注意传递给 json_decode 的第二个参数:
When true
, JSON objects will be returned as associative arrays;
你的情况:
$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>';
我很难找到这方面的资源,使用 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
)
)
我不明白为什么,理想情况下我希望它是一个数组,这样我就可以分配变量和 运行 一个循环来显示信息。
有什么想法吗?提前致谢。
将结果从字符串转换为 xml 对象 (simplexml_load_string):
$resultAsXml = simplexml_load_string($result);
然后使用json编码(json_encode, json_decode)将对象转换为数组:
$resultAsArray = json_decode(json_encode($resultAsXml), true);
注意传递给 json_decode 的第二个参数:
When
true
, JSON objects will be returned as associative arrays;
你的情况:
$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>';