You are here:  » How to parse results from within a function?


How to parse results from within a function?

Submitted by mangopudding on Wed, 2009-12-09 07:09 in

Hi there.

I'm playing around with a third-party REST API that returns XML for my virtual servers.

So far, I find out the status of my servers (if they are running or not) and also start or stop them by clicking on a hyper link.

What I want to do is be able to parse XML results *AFTER* I have stopped or started the server to get it's status. When I click on the link, it does spew back some XML on it's status for me - I just don't know how to grab and parse that.

See code below. Any help is appreciated. Thank-you! :)

<?php
   require("MagicParser.php");
$global_ORGID="";
$global_USERPASS="";
$myUserName=$_POST['UserID'];
$myPassword=$_POST['Password'];
$global_USERPASS="$myUserName".":"."$myPassword";
// ************************************************************************************************
// Get My Account Details
// ************************************************************************************************
$myAccountURL = "https://{$global_USERPASS}@api.BLANK.URL/oec/0.9/myaccount";
function myAccountInfo($record)
   {
global $global_ORGID;
$global_ORGID=$record["NS3:ORGID"];
   }
   MagicParser_parse($myAccountURL,"myAccountInfo", "xml|NS3:ACCOUNT/");
// ************************************************************************************************
// List My Current Servers
// ************************************************************************************************
$MyCurrentServersURL = "https://{$global_USERPASS}@api.BLANK.URL/oec/0.9/{$global_ORGID}/server";
   function listMyCurrentServers($item)
{
global $global_ORGID;
global $global_USERPASS;
print "<p><a href='https://".$global_USERPASS."@api.BLANK.URL/oec/0.9/".$global_ORGID."/server/".$item["ID"]."'>".$item["NAME"]."</a></p>";
print "<p>Server is currently ";
if ( $item["ISSTARTED"] == "true" )
{
print "RUNNING.<br />";
print "<p><a href='https://".$global_USERPASS."@api.BLANK.URL/oec/0.9/".$global_ORGID."/server/".$item["ID"]."?stop'>CLICK HERE TO STOP SERVER</a></p>";
}
else
{
print "STOPPED.<br />";
print "<p><a href='https://".$global_USERPASS."@api.BLANK.URL/oec/0.9/".$global_ORGID."/server/".$item["ID"]."?start'>CLICK HERE TO START SERVER</a></p>";
};
print "<HR>";
}
MagicParser_parse($MyCurrentServersURL,"listMyCurrentServers");
?>

Submitted by support on Wed, 2009-12-09 07:35

Hi,

In order to capture and parse the XML returned by the link to start / stop a server, you will need to make the script instigate the call rather than your own physical click on the link.

Since it looks like your PHP installation is capable of fopen()'ing an https URL this should be relatively straight forward. It looks like the server ID (along with UserID and Password of course) is the only parameter that needs to be passed through "back" to the script, so have a go with something like this:

<?php
require("MagicParser.php");
$global_ORGID="";
$global_USERPASS="";
$myUserName=$_POST['UserID'];
$myPassword=$_POST['Password'];
$global_USERPASS="$myUserName".":"."$myPassword";
// ************************************************************************************************
// Get My Account Details
// ************************************************************************************************
   
$myAccountURL "https://{$global_USERPASS}@api.BLANK.URL/oec/0.9/myaccount";
   function 
myAccountInfo($record)
   {
     global 
$global_ORGID;
     
$global_ORGID=$record["NS3:ORGID"];
   }
   
MagicParser_parse($myAccountURL,"myAccountInfo""xml|NS3:ACCOUNT/");
// ************************************************************************************************
// List My Current Servers
// ************************************************************************************************
$MyCurrentServersURL "https://{$global_USERPASS}@api.BLANK.URL/oec/0.9/{$global_ORGID}/server";
   function 
listMyCurrentServers($item)
{
global 
$global_ORGID;
global 
$global_USERPASS;
print 
"<p><a href='https://".$global_USERPASS."@api.BLANK.URL/oec/0.9/".$global_ORGID."/server/".$item["ID"]."'>".$item["NAME"]."</a></p>";
print 
"<p>Server is currently ";
if ( 
$item["ISSTARTED"] == "true" )
{
print 
"RUNNING.<br />";
print 
"<form method='post'>";
print 
"<input type='hidden' name='UserID' value='".$_POST["UserID"]."'>";
print 
"<input type='hidden' name='Password' value='".$_POST["Password"]."'>";
print 
"<input type='hidden' name='ItemID' value='".$item["ID"]."'>";
print 
"<input type='submit' name='submit' value='STOP' />";
print 
"</form>";
// print "<p><a href='https://".$global_USERPASS."@api.BLANK.URL/oec/0.9/".$global_ORGID."/server/".$item["ID"]."?stop'>CLICK HERE TO STOP SERVER</a></p>";
}
else
{
print 
"STOPPED.<br />";
print 
"<form method='post'>";
print 
"<input type='hidden' name='UserID' value='".$_POST["UserID"]."'>";
print 
"<input type='hidden' name='Password' value='".$_POST["Password"]."'>";
print 
"<input type='hidden' name='ItemID' value='".$item["ID"]."'>";
print 
"<input type='submit' name='submit' value='START' />";
print 
"</form>";
// print "<p><a href='https://".$global_USERPASS."@api.BLANK.URL/oec/0.9/".$global_ORGID."/server/".$item["ID"]."?start'>CLICK HERE TO START SERVER</a></p>";
};
print 
"<HR>";
}
function 
myStartStopRecordHandler($record)
{
  
// process response to start/stop command here
  
print_r($record);
}
if (
$_POST["submit"]=="START")
{
  
$StartStopURL "https://".$global_USERPASS."@api.BLANK.URL/oec/0.9/".$global_ORGID."/server/".$_POST["ItemID"]."?start";
  
MagicParser_parse($StartStopURL,"myStartStopRecordHandler");
}
elseif (
$_POST["submit"]=="STOP")
{
  
$StartStopURL "https://".$global_USERPASS."@api.BLANK.URL/oec/0.9/".$global_ORGID."/server/".$_POST["ItemID"]."?stop";
  
MagicParser_parse($StartStopURL,"myStartStopRecordHandler");
}
else
{
  
MagicParser_parse($MyCurrentServersURL,"listMyCurrentServers");
}
?>

...and then you can process the response to the start / stop command within the myStartStopRecordHandler function...

Hope this helps!
Cheers,
David.

Submitted by mangopudding on Fri, 2009-12-11 06:19

Hi David,

I''m getting an autodetect error when it tries to go into the myStartStopRecordHandler.

I'm guessing that it's because my $StartStopURL is a an Internet URL.

Any ideas on how I can get around this issue?

I was thinking of dynamically building the stop/start URLS when I call the listMyCurrentServers function and storing each of those URL's in an array.

Then print out the array with each URL that's wrapped around HTML so when it creates the page - it would allow me to click on a start/stop hyperlink.

Or maybe I should just hire you as a contractor to help me build my app. :)

- Simon.

Submitted by support on Fri, 2009-12-11 08:44

Hi Simon,

The first thing to do here is to print out the URL that is being constructed and double check that it looks OK. If all looks OK at that stage, then request the URL in your web browser and check that the XML response is as expected. If it is, then it may be that there are insufficient records for the XML to be autodetected correctly, in which case, could you possibly capture the XML to a file and email it to me and I'll take a look and let you know what Format String to use so that auto-detection is not necessary.

To print the constructed URL, in place of the following code to handle the "stop" command:

  $StartStopURL = "https://".$global_USERPASS."@api.BLANK.URL/oec/0.9/".$global_ORGID."/server/".$_POST["ItemID"]."?stop";
  MagicParser_parse($StartStopURL,"myStartStopRecordHandler");

...use:

  $StartStopURL = "https://".$global_USERPASS."@api.BLANK.URL/oec/0.9/".$global_ORGID."/server/".$_POST["ItemID"]."?stop";
  print $StartStopURL;
  MagicParser_parse($StartStopURL,"myStartStopRecordHandler");

...and the URL will then be displayed for you to check and request manually...

Cheers,
David.

Submitted by mangopudding on Fri, 2009-12-11 10:34

The response XML looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns6:Status xmlns:ns2="http://oec.api.BLANK.URL/schemas/organization" ..>
    <ns6:operation>Start Server</ns6:operation>
    <ns6:result>SUCCESS</ns6:result>
    <ns6:resultDetail>Server "Start" issued</ns6:resultDetail>
    <ns6:resultCode>REASON_0</ns6:resultCode>
</ns6:Status>

Submitted by support on Fri, 2009-12-11 11:01

Hi Simon,

As it stands, the .. in the openning element is resulting in the XML being invalid which stops the parse. However it's easy to remove using an str_replace() operation, after which the XML parses fine using the Format String xml|NS6:STATUS/

To do this, replace the start/stop sections above with:

  $StartStopURL = "https://".$global_USERPASS."@api.BLANK.URL/oec/0.9/".$global_ORGID."/server/".$_POST["ItemID"]."?stop";
  $xml = file_get_contents($StartStopURL);
  $xml = str_replace("..","",$xml);
  MagicParser_parse("string://".$xml,"myStartStopRecordHandler","xml|NS6:STATUS/");

...and

  $StartStopURL = "https://".$global_USERPASS."@api.BLANK.URL/oec/0.9/".$global_ORGID."/server/".$_POST["ItemID"]."?start";
  $xml = file_get_contents($StartStopURL);
  $xml = str_replace("..","",$xml);
  MagicParser_parse("string://".$xml,"myStartStopRecordHandler","xml|NS6:STATUS/");

Hope this helps!
Cheers,
David.

Submitted by mangopudding on Tue, 2009-12-15 08:30

Hi David,

Thanks for all your help!

- Simon.