setting a variable in php from an xml response

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
ihateevilbill
Forum Commoner
Posts: 29
Joined: Fri Nov 14, 2003 5:57 am

setting a variable in php from an xml response

Post by ihateevilbill »

Heres the do....

I have to send a request to a server which will then send back an xml file and display it in an iframe...thing is I want to add an XSLT to it...so how would I stop the response being shown in the IFRAME and instead setting it to a variable so that I can use Sablotron to output an XSLT's version of the file.

The php code im using is as follows
<?php
?>
<form action="http://195.57.250.36/barceloDS/interface/xml" target="resp1" method="post">
<table width="100%">
<tr>
<td width="50%"><font size="1"><b>Request</b></font></td>
<td width="50%"><font size="1"><b>Answer</b></font></td>
<tr>
<td>
<textarea class="request1" name="xml" rows="13" style="width:100%;">
<barceloDS_requests>

<request type="availability list" id="1">
<language_code>ING</language_code>
<agency>
<primary>888</primary>
<secondary>88</secondary>
<detail>888</detail>
<branch>1</branch>
</agency>
<contract></contract>
<check_in_date>20031220</check_in_date>
<check_out_date>20031227</check_out_date>
<location>
<destination_code>PMI</destination_code>
<zone_code></zone_code>
</location>
<establishment>
<code></code>
<category></category>
</establishment>
<board_type_code></board_type_code>
<occupancy>
<adults>2</adults>
<children>1</children>
<rooms>1</rooms>
</occupancy>
</request>

</barceloDS_requests>

</textarea>
</td>
<td>

<iframe name="resp1" style="width:100%; height:162"></iframe>

</td>
</tr>
<tr><td colspan="2" align="center"><input type="submit" value=">>>Send request>>>"></td></tr>
</table>
</form>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

<form action="<?=$PHP_SELF>" method="post"> 
<table width="100%"> 
<tr> 
<td width="50%"><font size="1"><b>Request</b></font></td> 
<td width="50%"><font size="1"><b>Answer</b></font></td> 
<tr> 
<td> 
<textarea class="request1" name="xml" rows="13" style="width:100%;"> 
<barceloDS_requests>
...................................
<div id=response>
<?php 
if(isset($_POST['xml'])){
  $ch=curl_init("http://195.57.250.36/barceloDS/interface/xml");
  curl_setopt($ch,CURLOPT_TIMEOUT,30);
  curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  curl_setopt($ch,CURLOPT_POST,true);
  curl_setopt($ch,CURLOPT_POSTFIELDS,"xml={$_POST['xml']}");
  $response=curl_exec($ch);
  if($err=curl_errno()) die("[$err] ".curl_error());
  //..............
  // process the $response here
  //..............
  echo $result_of_response_processing;
}
?> 
</div>
Remove <iframe> completely.
ihateevilbill
Forum Commoner
Posts: 29
Joined: Fri Nov 14, 2003 5:57 am

Post by ihateevilbill »

Superb m8, trying it as soon as Ive posted this bad-boy.

Cheers muchly,

Bill
ihateevilbill
Forum Commoner
Posts: 29
Joined: Fri Nov 14, 2003 5:57 am

Post by ihateevilbill »

ARRRGGGHHHH...
Still cant get it working...and I have been trying for about 3 hours now.

Ok, so here is what I have got:

Code: Select all

<form action="<?=$PHP_SELF?>" method="post">
<table width="740">
<tr>
<td width="100%"><font size="1"><b>Request</b></font></td>
<tr>
<td>
<textarea name="xml" rows="13" style="width:100%;">
<barceloDS_requests>
<language_code>ING</language_code>
-----------------------------------------------
</barceloDS_requests>
</textarea>
</td>
<td>
</td>
</tr>
<tr><td colspan="2" align="center"><input type="submit" value=">>>Send request>>>"></td></tr>
</table>
</form>

<div id=response>
<?php
if(isset($_POST['xml'])){
  $ch=curl_init("http://195.57.250.36/barceloDS/interface/xml");
  curl_setopt($ch,CURLOPT_TIMEOUT,30);
  curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  curl_setopt($ch,CURLOPT_POST,true);
  curl_setopt($ch,CURLOPT_POSTFIELDS,"xml={$_POST['xml']}"); 
  curl_exec($ch);
  if($err=curl_errno()) die("[$err] ".curl_error());

echo $response; //put in to show positive response

$xh = xslt_create();
$result = xslt_process($xh, $response, 'c.xsl');
xslt_free($xh);
echo $result;
curl_close ($ch);
}
?>
</div>
As you can see, I added the ? to the form action, I also added a line to echo the response to make sure a response was being grabbed.

Thing is I dont get a response (the echo comes back with no output), so I tried to output the $result to a new file (called c:\new.xml using the fopen command) and still nothing.

So, I am stuck again mate.

Hopefully you can tell where Im going wrong....people are now on my back asking for this solution, lol

Cheers again.

PS Just in case you havent seen the Sablotron lines (the 4 or so lines in the code that I have added), all its doing it attaching and xml file (in this case the $response) to an xslt file and outputing it, but I should still be getting an output of some sort by echo-ing the $response shouldnt I?)

MSN: ihateevilbill@hotmail.com if its easier (quicker) ;)
ihateevilbill
Forum Commoner
Posts: 29
Joined: Fri Nov 14, 2003 5:57 am

Post by ihateevilbill »

oops... the 'curl_exec($ch);' does actually contain '$response=' at the start of the line...musta accidently deleted it when posting.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

hmm... I made the following little script for testing:

Code: Select all

<?php
$request="<barceloDS_requests> 
<request type="availability list" id="1"> 
<language_code>ING</language_code> 
<agency> 
<primary>888</primary> 
<secondary>88</secondary> 
<detail>888</detail> 
<branch>1</branch> 
</agency> 
<contract></contract> 
<check_in_date>20031220</check_in_date> 
<check_out_date>20031227</check_out_date> 
<location> 
<destination_code>PMI</destination_code> 
<zone_code></zone_code> 
</location> 
<establishment> 
<code></code> 
<category></category> 
</establishment> 
<board_type_code></board_type_code> 
<occupancy> 
<adults>2</adults> 
<children>1</children> 
<rooms>1</rooms> 
</occupancy> 
</request> 
</barceloDS_requests>";
$ch=curl_init("http://195.57.250.36/barceloDS/interface/xml");
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch,CURLOPT_TIMEOUT,30);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,"xml=$request");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$result=curl_exec($ch);
echo $result;
?>
It gives me a bunch of xml... try it. just copy/paste and try.
ihateevilbill
Forum Commoner
Posts: 29
Joined: Fri Nov 14, 2003 5:57 am

Post by ihateevilbill »

Did I ever tell you 'YOU DA MAN!!!' (if you are a woman, then 'YOU DA GIRLY!!!'

Superb m8, thanks a lot...ill be able to xslt this result now.

Brilliant.
Post Reply