I have a PHP script that fetches information from a remote server using SOAP. After figuring out how to use SOAP, I got stuck on the response sent. I am unable to parse the data since it appears that my array is an object array. How would I be able to parse the data correctly?
Code:
<?php
$wsdl = 'https://192.168.1.10/requests.asmx?WSDL';
$trace = true;
$exceptions = false;
$xml_array['StartTime'] = "2014-01-27T00:00:00";
$xml_array['EndTime'] = "2014-09-27T23:59:00";
$login = 'test';
$password = 'test';
try
{
$client = new SoapClient($wsdl, array('login' => $login, 'password' => $password, 'trace' => $trace, 'exceptions' => $exceptions));
$response = $client->GetAll($xml_array);
}
catch (Exception $e)
{
echo "Error!";
echo $e -> getMessage ();
echo 'Last response: '. $client->__getLastResponse();
}
//echo $response->["Title"];
//var_dump($response);
?>
Response from Server:
[1]=> object(stdClass)#5 (19) { ["ID"]=> int(200)
["Title"]=> string(13) "Test" ["StartTimeUTC"]=> string(20) "2014-09-24 05:00:00Z"
["EndTimeUTC"]=> string(20) "2014-09-27 05:00:00Z" ["OwnerId"]=> int(10)
["UserName"]=> string(13) "testuser" ["FirstName"]=> string(7) "Test"
["LastName"]=> string(12) "User" ["Email"]=> string(27)
"[email protected]" ["ServiceType"]=> string(7) "Default" }
*Newest Code
$wsdl = 'https://192.168.1.10/requests.asmx?WSDL';
$trace = true;
$exceptions = false;
$xml_array['StartTime'] = "2014-01-27T00:00:00";
$xml_array['EndTime'] = "2014-09-27T23:59:00";
$login = 'test';
$password = 'test';
try
{
$client = new SoapClient($wsdl, array('login' => $login, 'password' => $password, 'trace' => $trace, 'exceptions' => $exceptions));
$response = $client->GetAll($xml_array);
}
catch (Exception $e)
{
echo "Error!";
echo $e -> getMessage ();
echo 'Last response: '. $client->__getLastResponse();
}
function objectToArray($response)
{
if (is_object($response))
$response = get_object_vars($response);
if (is_array($response))
return array_map(__FUNCTION__, $response1);
else
return $response;
}
$array = objectToArray($response);
echo $array['0']['Title'];
print_r($array);
Server Response from newest code:
Array ( [GetAll] => Array ( [Conference] => Array ( [0] => Array (
[ConferenceId] => 1 [Title] => Test [StartTimeUTC] => 2014-05-23 11:36:15Z
[EndTimeUTC] => 2014-05-23 12:06:15Z
[OwnerId] => 2 [UserName] => testuser [FirstName] => Test
[LastName] => User [Email] => [email protected]
[ServiceType] => Default )