I've had some difficulty finding resources on this, to connect to a SOAP API using PHP and easily attach the array results to variables for display purposes.
Here is the SOAP API XML of GetCostCenters function within the API:
<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>
Here is my 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>';
Outputs it all on one line in [any] like this:
stdClass Object
(
[GetCostCentersResult] => stdClass Object
(
[any] => 1OK4Adelaide6Derby33GT Perth7Hobart Branch46Lawns & Maintenance10London3Melbourne45NECA Apprentices17New York48Nursing NSW1Perth44Registration16Rio50Subiaco51Subiaco2Sydney42test - tester49TesterOM8Tom Price47Traffic Control NSW
)
)
and I can't figure out why, ideally I want it as an array so I can assign variables and run a loop to display the information.
Any ideas? Thanks in advance.
Convert the result from string to xml object (simplexml_load_string):
$resultAsXml = simplexml_load_string($result);
Then convert the object to array using the json encoding (json_encode, json_decode):
$resultAsArray = json_decode(json_encode($resultAsXml), true);
Note the second argument passed to json_decode:
When
true
, JSON objects will be returned as associative arrays;
In your case:
$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>';