Reputation: 163
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 )
Upvotes: 1
Views: 95
Reputation: 1649
1st solution
Use this function to convert an object
to an array
:
/**
* @param Obj The object to convert
* @return Array The converted array
*/
function objectToArray($obj)
{
if (is_object($obj))
$obj = get_object_vars($obj);
if (is_array($obj))
return array_map(__FUNCTION__, $obj);
else
return $obj;
}
2nd solution
For json object you might want to use json_decode:
json_decode($jsonObj);
From the documentation:
Return Values
Returns the value encoded in json in appropriate PHP type. Values true, false and null are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
3rd solution
Only if your object properties are public:
$array = (array) $object;
Upvotes: 2