0

I am writing a test script in php. I get back XML from the server in simplexml objects. Ive been parsing most fine, some using foreach loops and some directly from the elements. This one response just will not let me store the value to a PHP variable.

This is the specific response

 <xml>
   <status>1</status>
   <count>1</count>
   <device id="72220">
     <udid>99000146864366</udid>
     <devicename>Sprint Wwe</devicename>
     <created>2014-07-01 13:22:27</created>
   </device>
</xml>

Here is my parsing block

$cRes = $proxy->queryXML($section, 'retrieve', array("device_id" => "all"));

if($cRes->status == '1'){
    $dvcID = $cRes->device[0]['id']->__toString();
    $devicename = $cRes->device[0]->devicename->__toString();
    if(empty($dvcID)) return $do;//
    if(empty($devicename)) return $do2;//hacked break points
    $res = array("deviceid" => $dvcID, 
    "devicename" => $devicename);
    return $res;
}

The objective is to pull only id and devicename from the first device in an arbitrary amount of devices listed in the response. The php variables seem to not be empty as they dont return on my hacked break points(using notepad++). Ive tried casting them as (string) but they still show up with no data but do not fail the empty() test. Casting the id as an int turns it into a 0. What am I missing here?

The error response from the server is as follows

Last Query:Array
(
    [device_id] =>
    [devicename] =>
    [return_type] => xml
    [section] => devices
    [action] => update
    [api_key] => xxxxxxxxxxxxxxxxxx
)

Last Response<?xml version="1.0" encoding="utf-8"?>
<xml>
<status>0</status>
<error code="410">Required device POST variables not supplied</error>
</xml>
jcaron
  • 17,302
  • 6
  • 32
  • 46
Logicoeur
  • 1
  • 1
  • I'm not clear what result you're actually getting. i.e. what does `var_dump($res)` show? – IMSoP Aug 05 '14 at 22:07
  • Are your return statements typos? You check for empty($dvcID) and empty($devicename) but you return $do and $do2, which would be empty unless you defined them elsewhere – tommoyang Aug 05 '14 at 22:08
  • @tommoyang those are empty returns on purpose. Using notepad++ I just write in a return for a unique empty variable as a breakpoint. – Logicoeur Aug 05 '14 at 22:14
  • @IMSoP The $res array is passed to another fn that makes a request to the server. The server responds with an error code and the variables/values passed to it in the errored request. The failed request includes the values from $res but in those key/val pairs the value is empty – Logicoeur Aug 05 '14 at 22:16
  • @Logicoeur Woah, hang on there! When debugging, the first rule is to keep things as simple as possible, and break down the problem. If the problem is in this code, then we don't need to know what happens to `$res` next, just what is in it at the end of this snippet of code. If `var_dump($res)` here shows the right thing, you know this code is working, and something is going wrong later. So you could then ignore these lines, and look at the next piece of code, debug that in isolation, and so on. – IMSoP Aug 05 '14 at 22:19
  • Alright sorry about that. var_dump on any variable just fails the script. I dont know how else to view whats in the variable @IMSoP – Logicoeur Aug 05 '14 at 22:23
  • @Logicoeur What do you mean by "fails the script?" Causes an error message? Or [a white screen with no visible error message](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12772851#12772851)? – IMSoP Aug 05 '14 at 22:24
  • @IMSoP the script stops running and returns the Last query and Last response – Logicoeur Aug 06 '14 at 01:23
  • I think you might find some of the tips [in this help page](http://stackoverflow.com/help/mcve) useful. It sounds like you're still trying to debug this problem in the context of the whole program, rather than producing a small piece of code that tests only one thing at a time, perhaps forcibly exiting using [`die();`](http://php.net/die) to make the test clearer. For instance, you could replace the line `return $res;` with `var_dump($res); die;`, and there is no way for any normal code to run after it. – IMSoP Aug 06 '14 at 08:16
  • see https://eval.in/176997 the code you posted is doing what it is supposed to do... – michi Aug 07 '14 at 17:32

0 Answers0