Parsing array within XML result - php

I am having trouble understanding the proper syntax to print array results using the SimpleXMLElement. From my xml result i must ask the user to match themselves with one of the people found in the array, and am not sure what is the best way to do this.
Sample XML result:
[authentication] => SimpleXMLElement Object
(
[age] => SimpleXMLElement Object
(
[code] => 5
[ambiguous] => SimpleXMLElement Object
(
[person] => Array
(
[0] => SimpleXMLElement Object
(
[name] => Paul Foreman
[question] => SimpleXMLElement Object
(
[id] => dcalc3
[prompt] => What+do+the+%3Cb%3Elast+four%3C%2Fb%3E+digits+of+your+Social+Security+Number+add+up+to%3F
[answer] => 5
)
)
[1] => SimpleXMLElement Object
(
[name] => Paul Foreman
[question] => SimpleXMLElement Object
(
[id] => dcalc3
[prompt] => What+do+the+%3Cb%3Elast+four%3C%2Fb%3E+digits+of+your+Social+Security+Number+add+up+to%3F
[answer] => 6
)
)
)
)
)
)
Solution im looking for:
<?php
$string = $xml_result;
$xml = new SimpleXMLElement($string);
$is_age_code = $xml->authentication->{'age'}->code;
if($x_is_age_code == '5'){
// code 5 means more than one match found
// Ask user verification question
// If answer matches question
// Set current user as that person
}
?>
How can i find out how many 'Persons' are in the array and identify them with a number?

To count the number of persons use:
echo count($xml->authentication->age->ambiguous->person);
To access the subnodes of a person use
echo $xml->authentication->age->ambiguous->person[0]->question->prompt;
or with a loop:
foreach ($xml->authentication->age->ambiguous->person as $person) {
echo $person->question->prompt;
}

Related

wordpress shortcode is picking full XML instead of a single line

I am trying to create a wordpress shortcode which returns the price of a book from an API.
Ideally, I would like to be able to create a shortcode with this format [currency_isbn13] but for the moment I'd be happy to just create a fixed shortcode for each book and currency.
After various attempts, I was able to put together the following php code, but it is not working as it should
function Price() {
$isbn13 = 9783899735215;
$url = 'https://api.bookdepository.com/search/lookup?isbn13='.$isbn13.'&clientId={redact}&authenticationKey={redact}&IP={redact}&currencies=GBP';
$sxml = simplexml_load_file($url);
print_r($sxml);
return $sxml->price;
}
add_shortcode('isbn13', 'Price');
I would expect the shortcode to return the price of the book, but I get the following:
How can I fix this? I'm sure it is very simple but can't figure it out
SimpleXMLElement Object ( [resultset] => SimpleXMLElement Object ( [status] => Success [results] => 1 [totalResults] => 1 [currentPage] => 1 [totalPages] => 1 ) [items] => SimpleXMLElement Object ( [item] => SimpleXMLElement Object ( [identifiers] => SimpleXMLElement Object ( [isbn13] => 9783899735215 ) [url] => https://www.bookdepository.com/Crocodile-Newts-Axel-Hernandez/9783899735215 [biblio] => SimpleXMLElement Object ( [title] => Crocodile Newts [format] => Hardback ) [availability] => Available - dispatched from the UK in 4 business days [pricing] => SimpleXMLElement Object ( [price] => SimpleXMLElement Object ( [#attributes] => Array ( [currency] => GBP ) [selling] => 50.27 ) ) [contributors] => SimpleXMLElement Object ( [contributor] => SimpleXMLElement Object ( [name] => Axel Hernandez [roleDescription] => By (author) [url] => https://www.bookdepository.com/author/Axel-Hernandez ) ) ) ) )
Looks as though you need to expand
return $sxml->price;
as your structure is more complex. Looks as though
return (string)$sxml->items->item->pricing->price->selling;
The cast to (string) makes the value easier to use elsewhere.

Zoho CRM API simpleXMLelement get multiple product IDs

I am working with the Zoho CRM api and I was able to extract the product ID when only inserting one product, but can't figure out how to do it with multiple products. https://www.zoho.com/crm/help/api/insertrecords.html#Insert_Multiple_records
I convert the response to simpleXMLElement and I can get the first product ID easily with:
...curl stuff
$data = curl_exec($ch);
$xml = new SimpleXMLElement($data);
$product_id = $xml->result->recorddetail->FL[0];
The question is if I have multiple product ID's sent back how would I get each one in a loop as my code will only return the first product ID successfully. This is an example of the response from 2 products inserted in the api and the returned response:
SimpleXMLElement Object ( [#attributes] => Array ( [uri] =>
/crm/private/xml/Products/insertRecords ) [result] => SimpleXMLElement
Object ( [message] => Record(s) added successfully [recorddetail] => Array (
[0] => SimpleXMLElement Object ( [FL] => Array ( [0] => **2389399000000122065**
[1] => 2017-03-12 21:33:50 [2] => 2017-03-12 21:33:50 [3] =>
SimpleXMLElement Object ( [#attributes] => Array ( [val] => Created By ) )
[4] => SimpleXMLElement Object ( [#attributes] => Array ( [val] => Modified
By ) ) ) ) [1] => SimpleXMLElement Object ( [FL] => Array ( [0] =>
**2389399000000122066** [1] => 2017-03-12 21:33:50 [2] => 2017-03-12 21:33:50
[3] => SimpleXMLElement Object ( [#attributes] => Array ( [val] => Created
By ) ) [4] => SimpleXMLElement Object ( [#attributes] => Array ( [val] =>
Modified By ) ) ) ) ) ) )
Not sure if it shows up in bold but the two values enclosed in ** ** is what I am looking to extract.
The key to this is to understand that this:
$xml->result->recorddetail->FL[0];
Is just shorthand for this:
$xml->result[0]->recorddetail[0]->FL[0];
That should make it obvious that to access the 2nd recorddetail (with index 1), you could write this:
$xml->result->recorddetail[1]->FL[0];
Because of the magic SimpleXML provides you can also find out how many there are:
count($xml->result->recorddetail);
And most relevantly for your case, loop over them:
foreach ( $xml->result->recorddetail as $recorddetail ) {
$product_id = $recorddetail->FL[0];
}
As a final tip, you probably want the $product_id variable to hold an ordinary string, not a SimpleXML object; you get that with a "string cast", like this:
$product_id = (string)$recorddetail->FL[0];

How to display a value from a SimpleXML object (the array notation is confusing me)

I have a PHP file that uses cURL to retrieve some XML. I now want to retrieve a value from the XML but I cannot traverse to it as I am confused with the notation.
Here's my retrieved XML:
SimpleXMLElement Object
(
[#attributes] => Array
(
[uri] => /fruit/apple/xml/green/pipType
)
[result] => SimpleXMLElement Object
(
[JobOpenings] => SimpleXMLElement Object
(
[row] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[no] => 1
)
[FL] => Array
(
[0] => 308343000000092052
[1] => ZR_6_JOB
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[no] => 2
)
[FL] => Array
(
[0] => 308343000000091031
[1] => ZR_5_JOB
)
)
)
)
)
)
I have this XML stored in a variable called $xml using:
$xml = new SimpleXmlElement($data, LIBXML_NOCDATA);
Any help for how I can select the ZR_5_JOB element please?
I have tried countless times, the last effort I had was:
print_r($xml->result->JobOpenings->row[0]->FL[0]);
Could anybody please help?
(I know I will then need to do some iteration, but I'll deal with that later!)
First loop the JobOpenings rows to get each row separately and then you can access the childrens of that element in an easy way.
foreach($xml->result->JobOpenings->row as $item) {
echo $item->FL[0] . '<br>';
}

I'm trying to find an attribute using simpleXML

I have a simpleXML output of:
SimpleXMLElement Object
(
[#attributes] => Array
(
[version] => 2
)
[currentTime] => 2013-02-05 21:26:09
[result] => SimpleXMLElement Object
(
[rowset] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => characters
[key] => characterID
[columns] => name,characterID,corporationName,corporationID
)
[row] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => Wrytha Cy
[characterID] => 209668693
[corporationName] => Deep Core Mining Inc.
[corporationID] => 1000006
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => Eve Mae
[characterID] => 624980803
[corporationName] => Viziam
[corporationID] => 1000066
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => Wrytha
[characterID] => 709227913
[corporationName] => The Flying Tigers
[corporationID] => 669350666
)
)
)
)
)
[cachedUntil] => 2013-02-05 21:35:04
)
I would like to loop through with my php loop and get "name' and "characterID". I've trying something like:
$simpleXML = simplexml_load_string($xml);
foreach ($simpleXML->result->rowset->row as $row) {
print_r($row);
$name = $row['#attributes']['name'];
echo $name.'<br>';
}
but $name is not being set. It's gonna be something simple, just not seeing it in my haste and first time with simpleXML.
Attributes are accessed using the syntax $element['attribute_name'], so in your case, you need $row['name'].
It's important to remember that SimpleXML objects are kind of magic - the $element->child, $element_list[0] and $element['foo'] syntax overloads the normal PHP logic to be useful. Similarly, (string)$element will give you the full textual content of an element, however it is broken up in the actual XML.
As such, the print_r output will not give you a "real" view of the object, so should be used with care. There are a couple of alternative debug functions I've written here which give a more accurate idea of how the object will behave.

Syntax question: accessing a PHP object

I have an object returned from $xml = simplexml_load_file($file). I'm trying to access 'location-id' and 'item-id', but I can't figure out how to access those pieces of info. If it only nested once, I know I could do something like $xml->{'item-id'} but it doesn't seem to work. What is the syntax for this? Sorry for the lack of formatting.. that's how it was returned on my browser.
SimpleXMLElement Object (
[item] => Array (
[0] => SimpleXMLElement Object (
[#attributes] => Array (
[item-id] => AAA )
[locations] => SimpleXMLElement Object (
[location] => SimpleXMLElement Object (
[#attributes] => Array (
[location-id] => 111
)
[quantity] => 1
[pick-up-now-eligible] => false
)
)
)
[1] => SimpleXMLElement Object
[#attributes] => Array (
[item-id] => BBB
)
[locations] => SimpleXMLElement Object (
[location] => SimpleXMLElement Object (
[#attributes] => Array (
[location-id] => 111
)
[quantity] => 1
[pick-up-now-eligible] => false
)
)
)
)
)
Could somebody chime in? TIA!!
it'd be
$xml->item[0]->attributes('item-id');
$xml->item[0]->locations->location->attributes('item-id');
To access attributes in SimpleXML, array-style syntax can be used.
$element['attribute_name'];
The SimpleXMLElement::attributes() method is also available. The above would be:
$element->attributes()->attribute_name;
If the attribute is namespaced (e.g. blah:attribute_name) then you can provide that namespace (as the prefix, or URI) to the attributes() method.
$element->attributes('blah', TRUE)->attribute_name;
$element->attributes('http://example.org/blah')->attribute_name;
See the SimpleXML Basic Usage manual page for further information.
To put the above into practice for this individual question, to print the item-id attribute of the second <item> element, you could use:
echo $xml->item[1]['item-id'];
The example below loops over the <item> elements and prints their associated item-id and (the first) location-id values.
foreach ($xml->item as $item) {
$location = $item->locations->location;
echo 'Item ID: ' . $item['item-id'] . "\n";
echo 'Locations: ' . $location['location-id'] . "\n";
}

Categories