Jump to content

XML to Smarty


rjhsystems

Recommended Posts

Hello. I have this XML Code here

 

<response status="ok">
   <serviceRequestList>
       <serviceRequest>
           <accountManagerId></accountManagerId>
           <billable></billable>
           <billableTotal></billableTotal>
           <billingStatus></billingStatus>
           <customerContactEmail></customerContactEmail>
           <customerContactId></customerContactId>
           <customerContactName></customerContactName>
           <customerContactPhone></customerContactPhone>
           <customerContactPhoneMobile></customerContactPhoneMobile>
           <customerId></customerId>
           <customerLocationCity></customerLocationCity>
           <customerLocationCountry></customerLocationCountry>
           <customerLocationId></customerLocationId>
           <customerLocationName></customerLocationName>
           <customerLocationNotes></customerLocationNotes>
           <customerLocationPostalCode></customerLocationPostalCode>
           <customerLocationState></customerLocationState>
           <customerLocationStreetAddress></customerLocationStreetAddress>
           <customerLocationZone></customerLocationZone>
           <customerName></customerName>
           <dateTimeCreated></dateTimeCreated>
           <dateTimeClosed></dateTimeClosed>
           <description></description>
           <detailedDescription></detailedDescription>
           <dueDate></dueDate>
           <externalId></externalId>
           <priority></priority>
           <priorityLabel></priorityLabel>
           <serviceManagerId></serviceManagerId>
           <serviceRequestId></serviceRequestId>
           <status></status>
           <timeOpen_hours></timeOpen_hours>
           <type></type>
       </serviceRequest>
       <serviceRequest>
           ...
       </serviceRequest>
   </serviceRequestList>
</response>

 

It is from an API URL

 

I can call the XML From PHP using SimpleXML but i need a way to code the PHP so in the template file for the page it will create table row for each ServiceRequestID with the information from <serviceRequestId>, <description>, <status> <serviceManagerId> & <datetimecreated> and maybe if possible if <status> is closed instead of <datetimecreated> it shows <datetimeclosed>

 

I am only able to get to the point of echoing the XML using SimpleXML in PHP but thats as far as i can go, i can't get it to goto smarty for the foreach statement, i never really messed with XML

 

This is the PHP Code i am trying to use

<?php

define("CLIENTAREA",true);
//define("FORCESSL",true); # Uncomment to force the page to use https://

require("dbconnect.php");
require("includes/functions.php");
require("includes/clientareafunctions.php");

$pagetitle = $_LANG['clientareatitle'];
$breadcrumbnav = '<a href="index.php">'.$_LANG['globalsystemname'].'</a>';
$breadcrumbnav .= ' > <a href="mypage.php">My Page</a>'; 



initialiseClientArea($pagetitle,'',$breadcrumbnav);

# To assign variables to the template system use the following syntax.
# These can then be referenced using {$variablename} in the template.

$smartyvalues["variablename"] = $value; 

# Check login status
if ($_SESSION['uid']) {

 # User is logged in - put any code you like here

 # Retrieve User ID

 $result = mysql_query("SELECT companyname FROM tblclients WHERE id=".(int)$_SESSION['uid']);
 $data = mysql_fetch_array($result);
 $companyname = $data[0];
 $credentials = "specialapipasswordfrompackettrap:xxxxxxxxxxxxxxxxxxxxxxxxxxxx";

               // Read the XML to send to the Web Service
             $xml_data = "<request>
   <serviceRequestList>
       <listType>basic</listType>
      <customerName>$companyname</customerName>
   </serviceRequestList>
</request>";

$url = "https://app.packettrappsa.com/api/2.0/serviceRequests/list.aspx";
               $page = "api/2.0/serviceRequests/list.aspx";
               $headers = array(
               "POST ".$page." HTTP/1.0",
               "Content-type: text/xml;charset=\"utf-8\"",
               "Accept: text/xml",
               "Cache-Control: no-cache",
               "Pragma: no-cache",
               "SOAPAction: \"run\"",
               "Content-length: ".strlen($xml_data),
               "Authorization: Basic " . base64_encode($credentials)
       );

$ch = curl_init();
       curl_setopt($ch, CURLOPT_URL,$url);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt($ch, CURLOPT_TIMEOUT, 60);
       curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
       curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);

        // Apply the XML to our curl call
               curl_setopt($ch, CURLOPT_POST, 1);
               curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data); 
               $data = curl_exec($ch); 



       if (curl_errno($ch)) {
               print "Error: " . curl_error($ch);
       } else {
              // Show me the result
              curl_close($ch);
             // echo $data;
             $xml = simplexml_load_string($data);

   foreach($xml->serviceRequestList->serviceRequest as $servicerequest)
   {
     $srid = "$servicerequest->serviceRequestId";
     $srdesc = "$servicerequest->description";
     $srstatus = "$servicerequest->status";
     $srassigned = "$servicerequest->status";
     $srcreatedate = "$servicerequest->dateTimeCreated";

   }

        $sr = array(
	      "srid" => "$srid",
             "srdesc" => "$srdesc",
             "srstatus" => "$srstatus",
             "srassigned" => "$srassigned",
             "srcreatedate"=> "$srcreatedate"
		  );


        $smartyvalues["sr"] = $sr;

     //$smartyvalues["srid"] = $srid;
     //$smartyvalues["srdesc"] = $srdesc;
     //$smartyvalues["srstatus"] = $srstatus;
     //$smartyvalues["srassigned"] = $srassigned;
     //$smartyvalues["srcreatedate"] = $srcreatedate;
}


} else {

 # User is not logged in

}

# Define the template filename to be used without the .tpl extension

$templatefile = "servicerequests"; 

outputClientArea($templatefile);

?>

Edited by rjhsystems
Link to comment
Share on other sites

I am getting Closer..... (I think)

 

i got the php script to make an array however if there is more then one <servicerequest></servicerequest> Tag i get

 

Array
(
   [id] => <serviceRequestId>1001</serviceRequestId>
   [description] => <description>Computer will not turn on</description>
   [status] => <status>Closed</status>
   [asssigned] => <serviceManagerId>0</serviceManagerId>
   [createdate] => <dateTimeCreated>2012-09-14T18:42:40</dateTimeCreated>
   [closedate] => <dateTimeClosed>2012-09-14T18:49:58.347</dateTimeClosed>
)
Array
(
   [id] => <serviceRequestId>1005</serviceRequestId>
   [description] => <description>this is a test</description>
   [status] => <status>New</status>
   [asssigned] => <serviceManagerId>33443928</serviceManagerId>
   [createdate] => <dateTimeCreated>2012-10-06T05:57:08</dateTimeCreated>
   [closedate] => 
)

 

Thus when outputting to Smarty i get this

 

1005 this is a test New 2012-10-06T05:57:08

1005 this is a test New 2012-10-06T05:57:08

1005 this is a test New 2012-10-06T05:57:08

1005 this is a test New 2012-10-06T05:57:08

1005 this is a test New 2012-10-06T05:57:08

1005 this is a test New 2012-10-06T05:57:08

 

Instead of

 

1001 Computer will not turn on Closed 2012-09-14T18:42:40

1005 this is a test New 2012-10-06T05:57:08

 

This is the PHP Code

 

<?php

define("CLIENTAREA",true);
//define("FORCESSL",true); # Uncomment to force the page to use https://

require("dbconnect.php");
require("includes/functions.php");
require("includes/clientareafunctions.php");

$pagetitle = $_LANG['clientareatitle'];
$breadcrumbnav = '<a href="index.php">'.$_LANG['globalsystemname'].'</a>';
$breadcrumbnav .= ' > <a href="mypage.php">My Page</a>'; 



initialiseClientArea($pagetitle,'',$breadcrumbnav);

# To assign variables to the template system use the following syntax.
# These can then be referenced using {$variablename} in the template.

$smartyvalues["variablename"] = $value; 

# Check login status
if ($_SESSION['uid']) {

 # User is logged in - put any code you like here

 # Retrieve User ID

 $result = mysql_query("SELECT companyname FROM tblclients WHERE id=".(int)$_SESSION['uid']);
 $data = mysql_fetch_array($result);
 $companyname = $data[0];
 $credentials = "xxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxx";

               // Read the XML to send to the Web Service
             $xml_data = "<request>
   <serviceRequestList>
       <listType>basic</listType>
      <customerName>$companyname</customerName>
   </serviceRequestList>
</request>";

$url = "https://app.packettrappsa.com/api/2.0/serviceRequests/list.aspx";
               $page = "api/2.0/serviceRequests/list.aspx";
               $headers = array(
               "POST ".$page." HTTP/1.0",
               "Content-type: text/xml;charset=\"utf-8\"",
               "Accept: text/xml",
               "Cache-Control: no-cache",
               "Pragma: no-cache",
               "SOAPAction: \"run\"",
               "Content-length: ".strlen($xml_data),
               "Authorization: Basic " . base64_encode($credentials)
       );

$ch = curl_init();
       curl_setopt($ch, CURLOPT_URL,$url);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt($ch, CURLOPT_TIMEOUT, 60);
       curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
       curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);

        // Apply the XML to our curl call
               curl_setopt($ch, CURLOPT_POST, 1);
               curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data); 
               $data = curl_exec($ch); 



       if (curl_errno($ch)) {
               print "Error: " . curl_error($ch);
       } else {
              // Show me the result
              curl_close($ch);
             // echo $data;


$xml = simplexml_load_string($data);

 foreach($xml->serviceRequestList->serviceRequest as $service) {
   $srid = $service->serviceRequestId->asXML();
   $srdesc = $service->description->asXML();
   $srstatus = $service->status->asXML();
$srassignedto = $service->serviceManagerId->asXML();
$srcreatedate = $service->dateTimeCreated->asXML();
$srclosedate = $service->dateTimeClosed->asXML();

$result = array('id' => $srid, 'description' => $srdesc, 'status' => $srstatus, 'asssigned' => $srassignedto, 'createdate' => $srcreatedate, 'closedate' => $srclosedate);



        $smartyvalues["sr"] = $result;


}

  }         
} else {

 # User is not logged in

}

# Define the template filename to be used without the .tpl extension

$templatefile = "servicerequests"; 

outputClientArea($templatefile);

?>

 

And This is The TPL Code

 

<a name="content"></a>
<div id="contentWrapper">
<div id="contentArea">		
<div class="post" id="post-71">	
<div class="postHeader">		
<h2 class="postTitle"><span></span>My Service Requests</h2>
</div>	<div class="postContent">
<table class="grid" cellspacing="0" rules="cols" border="1" id="dgSRList" style="width:100%;border-collapse:collapse;">
<tbody><tr>
	<th scope="col">
                       <font>No.</font>
                    </th>
              <th scope="col">
                       <font>Description</font></th>
             <th scope="col">
                       <font>status</font></th>
             <th scope="col" style="white-space:nowrap;">
                      <font>Assigned To</font></th>
             <th scope="col">
                       <font>Created</font></th>
</tr>
{foreach from=$sr item=id}
<tr>
	<td style="width:60px;white-space:nowrap;">
                       <a class="bold" href="servicerequest.php?id={$sr.id}">{$sr.id}</a>
                   </td><td>
                       <a class="bold" href="servicerequest.php?id={$sr.id}">{$sr.description}</a>

                   </td><td>
                       <span id="SRStatus_1000"></span>{$sr.status}</td><td>{$sr.assignedto}
                       </td><td style="width:90px;white-space:nowrap;">
                      {$sr.createdate}
                   </td>
</tr>
{/foreach}
</tbody></table>
</form>
{include file="$template/sidebar.tpl"}

 

Any Ideas????

Link to comment
Share on other sites

Apologies if I am totally off the mark here, but I'm pretty drunk now ;) But shouldn't

 

         $smartyvalues["sr"] = $result; 

 

be

 

         $smartyvalues["sr"][] = $result;

 

?

 

(I've not used Smarty before for this, but it seems otherwise the variable is just overwriting itself each time...)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated