Accessing a simplexml element within a simplexml element - php

I think theres something more going on here but it looks like certain data available in the raw xml is unavailable to me after it gets set in the simplexml element via my soap call.
Here's the raw information I get back from the soap call (retrieved w/ soapUI), Please note the e_mail element in particular.
<GetSubscriberData_ByDrupal_IdResult><![CDATA[
<SubscriberDataRoot>
<SubscriberData>
<city>MEDIA</city>
<state>PA</state>
<zip>19063-4112</zip>
<country>USA</country>
<phone>1231231244</phone>
<e_mail>some_email#somewhere.com</e_mail>
</SubscriberData>
</SubscriberDataRoot>]]>
</GetSubscriberData_ByDrupal_IdResult>
and here is the SimpleXMLElement that I var_dump'd after it was yielded from my SoapClient call.
object(SimpleXMLElement)#26 (1) {
["SubscriberData"]=>
object(SimpleXMLElement)#33 (29) {
["city"]=>
string(5) "MEDIA"
["state"]=>
string(2) "PA"
["zip"]=>
string(10) "19063-4112"
["country"]=>
string(3) "USA"
["phone"]=>
string(10) "1231231234"
["e_mail"]=>
object(SimpleXMLElement)#36 (0) {
}
}
}
I can access most of the data as expected with something akin to $soap_data->SubscriberData->city however notable the e_mail element is not available directly, its another SimpleXMLElement. I've tried iterating over it, using asXML, __toString, casting to a (string)..
(for instance...)
php> var_dump($acct->SubscriberData->e_mail);
object(SimpleXMLElement)#38 (0) {
}
php> var_dump($acct->SubscriberData->e_mail->asXML());
string(17) "<e_mail></e_mail>"
php> var_dump($acct->SubscriberData->e_mail->__toString());
string(0) ""
php> var_dump((string) $acct->SubscriberData->e_mail);
string(0) ""
but I can't access it like any of the other string valued elements. I want to be able to get the string value such as $email = $acct->SubscriberData->e_mail like the rest of the elements. Thanks in advance.

I'm not sure I can test/reproduce your object structure without your soap stuff, but if I took the raw XML and tried to process it, I'd have to process the CDATA block separately.
$GetSubscriberData_ByDrupal_IdResult = simplexml_load_string($xmlSource);
$SubscriberDataRoot = simplexml_load_string((string) $GetSubscriberData_ByDrupal_IdResult);
$email = (string) $SubscriberDataRoot->SubscriberData->e_mail;
echo $email; // assigned to var just for demonstration

Related

PHP: how can I access JSON object

I'm sending this JSON:
[{"tipo":""},{"activo":""},{"titulo":"Servicoasd B"},{"texto":"asdasdasd"}]
to a php file via post method.
There, i do
$obj = json_decode($_POST['sentJson']);
However, I seem to be unable to access the elements of the JSON.
var_dump(($obj));
Shows the object:
array(4) {
[0]=>
object(stdClass)#2 (1) {
["tipo"]=>
string(0) ""
}
[1]=>
object(stdClass)#3 (1) {
["activo"]=>
string(0) ""
}
[2]=>
object(stdClass)#4 (1) {
["titulo"]=>
string(9) "Servico B"
}
[3]=>
object(stdClass)#5 (1) {
["texto"]=>
string(6) "asdasd"
}
}
But if I try
$obj['texto'];
$obj->{'texto'};
$obj[0]['texto'];
$obj[0];
It shows "undefined index texto" or "trying to get property of non object in" and the last one "Object of class stdClass could not be converted to string in". I'm very new to PHP, but still I can't seem to notice what I'm doing wrong. Any help would be appreciated.
Your JSON is a serialized array of four completely different objects, so when you run json_decode, that's what you get: an array.
If you want to access your objects inside that array, access them like you would any other indexed array:
$list = json_decode(...);
foreach($list as $obj) {
var_dump($obj)
}
Or target them explicitly using plain old numerical access.
$list = json_decode(...);
$last = $list[3];
$text = $last->texto;
But really the question you should be asking is why this is the JSON you get. An array with completely different objects at each position is terrible data, and should be fixed.

Accessing array with nested objects

I have an api wrapper i am using that returns something like this
object(stdClass)#7 (1) {
[0]=>
object(stdClass)#6 (2) {
["contactId"]=>
string(2) "nV"
["email"]=>
string(31) "email#domain.com"
}
}
how do i access the email part with PHP
Cast your API returned data to an array.
For example you are saving API returned data in $result variable. Cast it to an array.
$arrayResult = (array) $result;
echo $arrayResult[0]->email;
Try this.

php, from json decode to individual variables

I am trying to go from a cur request to a page with some info.
I have the curl working but I have trouble going from the decode json to individual php variables. the conten after json_decode is:
object(stdClass)#1 (2) { ["response"]=> object(stdClass)#2 (2) { ["request"]=> string(20) "mailboxes/status/get" ["result"]=> string(1) "0" } ["status"]=> string(7) "success" }
I need the value of result which is 0 here.
Thanks in advance.
$result = $data->response->result;
Assuming the variable $data is where you stored your json_decode. It returns an instance of stdClass, and viewing the vardump, you can see the structure and get the data you want.

Accessing a specific XML-node with PHP using SimpleXML

lately i ran into a problem using simplexml.
What i want to do is to get a value of a nested node that occurs multiple times.
The xml looks somewhat like this:
<response>
<album id="123">
[...]
<duration>
<value format="seconds">2576</value>
<value format="mm:ss">42:56</value>
<value format="hh:mm:ss">00:42:56</value>
<value format="xs:duration">PT42M56S</value>
</duration>
[...]
</album>
</response>
Specifically i need the value of the the <value format="hh:mm:ss"> node.
So I have a reference to the object that looks somewhat like this:
$this->webservice->album->duration->value;
Now, if i var_dump this the result will be:
object(SimpleXMLElement)#117 (5) {
["#attributes"]=> array(1) {
["format"]=> string(7) "seconds"
}
[0]=> string(4) "2576"
[1]=> string(5) "42:56"
[2]=> string(8) "00:42:56"
[3]=> string(8) "PT42M56S"
}
I do not understand this output, since it takes the format-attribute of the first node (seconds) and continues with the node-values in the array, while ignoring the format-attribute of the following nodes completely.
Furthermore, if i do the following:
$this->webservice->album->duration->value[2];
it results in:
object(SimpleXMLElement)#108 (1) {
["#attributes"]=> array(1) {
["format"]=> string(8) "hh:mm:ss"
}
}
where i haven't got a value to address at all.
I tried to use xpath instead too in the following way:
$this->webservice->album->duration->xpath('value[#format="hh:mm:ss"]');
which results in:
array(1) {
[0]=> object(SimpleXMLElement)#116 (1) {
["#attributes"]=> array(1) {
["format"]=> string(8) "hh:mm:ss"
}
}
}
So my question is:
What am I doing wrong? xD
Thanks in advance for any helpful advice :)
Your mistake is in trusting var_dump too completely, rather than trying to use the elements based on the examples in the manual.
On your first attempt, you accessed $duration_node->value; this can be used in a few different ways:
if you iterate over it with foreach($duration_node->value as $value_node), you get each of the <value> elements in turn
you can access specific elements by index, e.g. $duration_node->value[2] for the 3rd element
if you treat it as a single element, SimpleXML assumes you want the first element, i.e. echo $duration_node->value is the same as echo $duration_node->value[0]
Your second example worked fine - it found the <value> element with an attribute format="hh:mm:ss". The xpath() method always returns an array, so you need to check that it's not empty then look at the first element.
Once you have the right element, accessing its text content is as simple as casting it to a string ((string)$foo), or passing it to something that always needs a string (e.g. echo).
So this would work:
$xpath_results = $this->webservice->album->duration->xpath('value[#format="hh:mm:ss"]');
if ( count($xpath_results) != 0 ) {
$value = (string)$xpath_results[0];
}
As would this:
foreach ( $this->webservice->album->duration->value as $value_node ) {
if ( $value_node['format'] == 'hh:mm:ss' ) {
$value = (string)$value_node;
break;
}
}

PHP JSON decoding - accessing object

I'm trying to extract a value from a JSON string that's in my program. The output of var_dump($obj); is this:
object(stdClass)#1 (1) {
["BTC"]=>
object(stdClass)#2 (2) {
["value"]=>
float(403.645)
["currency"]=>
string(3) "GBP"
}
}
What I want to access is the value (currently 403.645 in this example), but I can't work out how to do it.
I've tried $obj->value and other combinations, but get nowhere; it appears to me that this is an object inside an object and as a result I can't find out how to access it. Any help would be appreciated!

Categories