?
Solved

Formatting XML results

Posted on 2007-03-30
8
Medium Priority
?
175 Views
Last Modified: 2010-05-18
I'm using a PHP and XML script to connect to a remote server and retrive data. I have most of it setup, but I'm trying to format the data that gets returned.

For example, if I use

var_dump($response);

to dump my results, I get something like the following:

object(DomainInfDataResponse)#15 (12) { ["name:private"]=>  string(14) "domainname.pl" ["status:private"]=>  string(6) "Active"  ["nss:private"]=>  array(2) { [0]=>  object(DomainNs)#20 (2) { ["name:private"]=>  string(15) "ns1.domainname.pl" ["ip:private"]=>  string(0) "" } [1]=>  object(DomainNs)#22 (2) { ["name:private"]=>  string(15) "ns2.domainname.pl" ["ip:private"]=>  string(0) "" } } ["holder:private"]=>  object(DomainHolder)#23 (4) { ["class:private"]=>  string(11) "Company" ["remark:private"]=>  string(21) "changed admin by name" } ["admins:private"]=>  array(1) { [0]=>  string(10) "NOC-453" } ["techs:private"]=>  array(1) { [0]=>  string(10) "NOC-453" } ["account:private"]=>  string(8) "00000161" ["regDate:private"]=>  string(10) "2001-06-27" ["renDate:private"]=>  string(10) "2007-06-27" ["result:private"]=>  object(Result)#6 (5) { ["code:private"]=>  string(4) "1000"  } ["type:protected"]=>  int(2) }

To retrive a single value from the response, I thought all I needed was:
$domain = $response=>name;

but that doesn't work.

How would I call a single variable?
0
Comment
Question by:thunder99
  • 3
  • 3
  • 2
8 Comments
 
LVL 4

Expert Comment

by:secondv
ID: 18823137
PHP5?

Could you provide some code, or give the output from:

print_r($response);
0
 

Author Comment

by:thunder99
ID: 18823363
Hi Secondv

Yes, it's php5.

print_r($response); gives:

DomainInfDataResponse Object ( [name:private] => domainname.pl [status:private] => Active [billingStatus:private] => Billable [nss:private] => Array ( [0] => DomainNs Object ( [name:private] => ns1.domainname.pl [ip:private] => ) [1] => DomainNs Object ( [name:private] => ns2.domainname.pl [ip:private] => ) ) [holder:private] => DomainHolder Object ( [class:private] => Company [remark:private] => changed admin by name ) [admins:private] => Array ( [0] => NOC-453 ) [techs:private] => Array ( [0] => NOC-453 ) [account:private] => 00000161 [regDate:private] => 2001-06-27 [renDate:private] => 2007-06-27 [result:private] => Result Object ( [code:private] => 1000 [msg:private] => Command completed successfully [value:private] => [reasonCode:private] => [reasonMsg:private] => ) [type:protected] => 2 )["type:protected"]=>  int(2) }

I can't really post the code as it's coming from lots of config files that are been called once.

The XML command that's been created looks like:

$command = new DomainInfoCommand("domainname.pl");
$response = CommandProcessor::process($command);
$result = $response->getResult();

//Test it//
var_dump($response);
print_r($response);

I actually had it working before, and then didn't save some changes and can't remember how I did it.


0
 
LVL 4

Accepted Solution

by:
secondv earned 1000 total points
ID: 18823570
To get a value:

echo $response->name;

However, if the variables are private "[name:private]" you won't be able to access it that way.

(Fatal error: Cannot access private property DomainInfDataResponse::$name in xxxx on line xx)

What you could do, is add a function inside the class that will return a given value.. such as:

public function get_value($var)
{
      return $this->$var;
}

$response->get_value('name');


0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Expert Comment

by:wakemup
ID: 18824661
You can also use a generic __get($varname) function so:

function __get($varname) {

}
0
 
LVL 2

Expert Comment

by:wakemup
ID: 18824672
Sorry:

function __get($varname) {
  return $this->$varname;
}

This should work in php 5.0+
0
 
LVL 2

Assisted Solution

by:wakemup
wakemup earned 1000 total points
ID: 18824692

You can then just call '$this->Fred' and the __get function gets called automatically in the background and the value is returned without you having to do a get_fred, get_john function or a get($varname) and explicitly saying which variable you want. PHP 5 is cool like this.

There is also a __set($varname, $varvalue) which does the same the other way. Those are two underscores by the way.
0
 

Author Comment

by:thunder99
ID: 18825772
Can't get any of those methods to work. I'll go through some of the other files that are been called to see if it gives any hints as to what I need.
0
 

Author Comment

by:thunder99
ID: 19125851
Sorry for the delay in getting back to this question. Both of the solutions offered by secondv and wakemup work, so I'm splitting the points.

Again apoligies for leaving it open so long.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question