Solved

Formatting XML results

Posted on 2007-03-30
8
162 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 250 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
 
LVL 2

Expert Comment

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

function __get($varname) {

}
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 250 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now