Reputation: 1067
I've written an API using a simple but fairly long query string request. The requester can designate an XML or HTML response.
The query is received and the data is processed. The HTML simply echos back a response. In order to avoid redundant code, I use a JavaScript redirect to open and process the XML. That looks like this:
$QString = '';
foreach($XMLItems as $key => $value)
{
$QString .= $key.'^'.$value.'|';
}
$QString = substr($QString,0,-1);
Redirect('XMLProcess.php?Q='.$QString);
Redirect() is a simple function that runs javascript:
function Redirect($n)
{
die("<script type=\"text/javascript\">window.location='$n';</script>");
}
The XML construction looks like this:
<?php
$dom = new DOMDocument("1.0");
header ("Content-Type:text/xml");
$QArray = explode('|',$_REQUEST[Q]);
foreach($QArray as $value)
{
$x = explode('^',$value);
$XMLItems[$x[0]] = $x[1];
}
$root = $dom->createElement("Data");
$dom->appendChild($root);
foreach($XMLItems as $key => $value)
{
$key = $dom->createElement($key);
$root->appendChild($key);
$variable = $dom->createTextNode($value);
$key->appendChild($variable);
}
echo $dom->saveXML();
?>
I'm pretty ignorant as far as API's and what someone may be using on the receiving end. I have a client who is asking for a 302 redirect and location header. I am assuming my Redirect() function may be throwing his software off, but I don't really know. Of course, I cannot redirect to the XML file immediately, as the incoming data needs to be processed first. So I am trying to wrap my mind around what the client needs without duplicating the processing in a second file. And, since I am in the dark, the Redirect may not be the problem, anyway.
Upvotes: 0
Views: 214
Reputation: 39434
To emit a 302 Redirect (or indeed any status code), you need to use header
. Per the docs:
The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set.
which would be implemented like:
function Redirect($n)
{
header("Location: $n");
exit(0);
}
As noted in the comments, since your script is emitting output prior to the invocation of Redirect
, you'll need to use ob_start
(and friends) to capture that output and emit it after the header has been sent:
ob_start();
// ...
$QString = '';
foreach($XMLItems as $key => $value)
{
$QString .= $key.'^'.$value.'|';
}
$QString = substr($QString,0,-1);
Redirect('XMLProcess.php?Q='.$QString);
function Redirect($n)
{
header("Location: $n");
$contents = ob_get_clean();
// echo $contents; // if you want or need to
// you might also consider leaving this, but clients will
// honor the 302 before executing any Javascript
die("<script type=\"text/javascript\">window.location='$n';</script>");
}
All of this said, though, the code is feeling pretty janky. If at all possible, consider refactoring the HTML and XML versions so that the main work of the code is done in an included helper, and the HTML and XML "front-ends" just focus on rendering the output in the desired format.
Upvotes: 1