Note that there are some explanatory texts on larger screens.

plurals
  1. POphp xpath problems
    primarykey
    data
    text
    <p>I'm doing a cURL POST and get the error response back, parse it into an array but having issues with xpath now.</p> <p>// XML</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;errors xmlns="http://host/project"&gt; &lt;error code="30" description="[] is not a valid email address."/&gt; &lt;error code="12" description="id[] does not exist."/&gt; &lt;error code="3" description="account[] does not exist."/&gt; &lt;error code="400" description="phone[] does not exist."/&gt; &lt;/errors&gt; </code></pre> <p>// Function / Class</p> <pre><code>class parseXML { protected $xml; public function __construct($xml) { if(is_file($xml)) { $this-&gt;xml = simplexml_load_file($xml); } else { $this-&gt;xml = simplexml_load_string($xml); } } public function getErrorMessage() { $in_arr = false; $el = $this-&gt;xml-&gt;xpath("//@errors"); $returned_errors = count($el); if($returned_errors &gt; 0) { foreach($el as $element) { if(is_object($element) || is_array($element)) { foreach($element as $item) { $in_arr[] = $item; } } } } else { return $returned_errors; } return $in_arr; } } </code></pre> <p>// Calling function</p> <pre><code>// $errorMessage is holding the XML value in an array index // something like: $arr[3] = $xml; $errMsg = new parseXML($arr[3]); $errMsgArr = $errMsg-&gt;getErrorMessage(); </code></pre> <p>What I would like is all the error code and description attribute values</p> <p>EDIT:</p> <p>OK this is print_r($this->xml,true);</p> <pre><code>SimpleXMLElement Object ( [error] =&gt; Array ( [0] =&gt; SimpleXMLElement Object ( [@attributes] =&gt; Array ( [code] =&gt; 30 [description] =&gt; [] is not a valid email address. ) ) [1] =&gt; SimpleXMLElement Object ( [@attributes] =&gt; Array ( [code] =&gt; 12 [description] =&gt; Id[12345] does not exist. ) ) [2] =&gt; SimpleXMLElement Object ( [@attributes] =&gt; Array ( [code] =&gt; 3 [description] =&gt; account[] does not exist. ) ) [3] =&gt; SimpleXMLElement Object ( [@attributes] =&gt; Array ( [code] =&gt; 400 [description] =&gt; phone[] does not exist. ) ) ) ) </code></pre> <p>for the life of me I can't figure out why I can get the code and description, any thoughts?</p> <p>EDIT #2 Okay so I guess I will break it down.</p> <p>I'm using cURL to POST a request to one of our servers, I parse out the HTTP response headers and xml (if xml is returned). each line in the header/xml I explode into an array. so if there is an error I see an extra index to the array. I then do something like this.</p> <pre><code>$if_err_from_header = $http_return_response[10]; // I know that index 10 is where if any the error message in xml is (the one posted above). </code></pre> <p>after that I do this:</p> <pre><code>$errMsg = new parseXML($if_err_from_header); $errMsgArr = $errMsg-&gt;getErrorMessage(); </code></pre> <p>still I can not get the code and description from the attributes in error, what am I missing?</p> <p>EDIT #3 Okay why does this work?</p> <pre><code>$in_arr = false; // This returns all the code attributes $el = $this-&gt;xml-&gt;xpath("//@code"); # if $el is false, nothing returned from xpath(), set to an empty array $el = $el == false ? array() : $el; foreach($el as $element) { $in_arr[] = array("code" =&gt; $element["code"], "description" =&gt; $element["description"]); } return $in_arr; </code></pre> <p>EDIT #4:</p> <p>Okay this gets that values I want but it's kinda a hack, would like to select specific elements but...</p> <pre><code>$el = $this-&gt;xml-&gt;xpath("//*"); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload