Jump to content

[SOLVED] Formatting XML Requests


Bifter

Recommended Posts

Hi and thanks for looking,

 

I send a request to an ADSL provider and a response is sent back to the browser in XML with this code:

 

<?php
function httpsPost($Url, $strRequest)
{
   // Initialisation
   $ch=curl_init();
   // Set parameters
   curl_setopt($ch, CURLOPT_URL, $Url);
   // Return a variable instead of posting it directly
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   // Active the POST method
   curl_setopt($ch, CURLOPT_POST, 1) ;
   // Request
   curl_setopt($ch, CURLOPT_POSTFIELDS, $strRequest);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
   // execute the connexion
   $result = curl_exec($ch);
   // Close it
   curl_close($ch);
   return $result;
}
$url = 'xml.xps.murphx.com';
$strRequest = utf8_encode('<?xml version="1.0"?>
<Request module="XPS" call="availability" id="8c4566926694d7227d9d73fd8c2acc5b"
version="2.0.1">
<block name="auth">
<a name="username" format="text">xxxxxx</a>
<a name="password" format="password">xxxxxxx</a>
<a name="client-id" format="counting">xxx</a>
</block>
<a name="postcode" format="postcode">LU2 0JJ</a>
<a name="detailed" format="yesno">Y</a>
<a name="order-type" format="text">provide</a>
</Request>');
$Response = httpsPost($url, $strRequest);

echo $Response;

?>

 

This will output the following in the browser:

<?xml version="1.0" ?> 
- <Response id="8c4566926694d7227d9d73fd8c2acc5b">
- <block name="availability">
  <a name="quick-result" format="counting">5</a> 
  <a name="postcode" format="postcode">LU2 0JJ</a> 
- <block name="classic-qualification">
  <a name="result-code" format="text">Z</a> 
  <a name="fixed-rate" format="text">G</a> 
  <a name="likely-max-speed" format="counting">2097152</a> 
  <a name="rate-adaptive" format="text">G</a> 
  </block>
- <block name="exchange">
  <a name="code" format="text">SMLT</a> 
  <a name="name" format="text">LUTON</a> 
  <a name="state" format="text">E</a> 
  <a name="llu" format="yesno">Y</a> 
  </block>
- <block name="max-qualification">
  <a name="rate-adaptive" format="text">G</a> 
  <a name="likely-max-speed" format="counting">4096000</a> 
  </block>
  </block>

 

My problems being:

 

1 - how do I get only certain parts of the XML response to display in the browser?

2 - how do I style the responses in XSL - I can style a static XML page but can't get this to work through PHP as a request?

3 - Can you convert each individual xml response such as <block name="exchange"> -> <a name="code"> into a variable? if so how?

 

Would be very grateful for anybody assistance in this.

 

Thanks,

B.

 

Link to comment
https://forums.phpfreaks.com/topic/157957-solved-formatting-xml-requests/
Share on other sites

<?php
$Response = httpsPost($url, $strRequest);
// create xml object from response
$xmlObj = simplexml_load_string($Response);
?>

 

You can then work through the elements as an array. Checkout some of the examples

http://uk2.php.net/manual/en/function.simplexml-load-string.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.