Question

I am attempting to parse a file downloads page, and then depending upon what arguement was passed in the URL, redirect them to the appropriate download file automatically. Everything works fine when I echo it, but when I attempt to use header() it does nothing.

<?php

require_once('simple_html_dom.php');

$html = file_get_html('http://dl.bukkit.org/downloads/craftbukkit/');

$dom = new DOMDocument;
libxml_use_internal_errors(true);
//echo $dom->loadHTML($html) ? "success<br/>" : "failed<br/>";
libxml_clear_errors();
$dom->preserveWhiteSpace = true;

$version = $_GET['version'];

if($version == "beta")
{
    foreach ($dom->getElementsByTagName('div') as $element)
    {
        if($element->getAttribute('class') == "downloadButton chan-rb  mini")
        {
            foreach ($element->getElementsByTagName('a') as $link)
            {
                if( $link->getAttribute('class') == "tooltipd")
                {
                    $lnk = $link->getAttribute('href')."<br/>";
                    $url = "Location:http://dl.bukkit.org".$lnk;
                    header($url);
                }
            }
        }
    }
}

?>

I believe I am running into the issue of the Headers already being sent, although I am not using any HTML prior to this. Are there any good alternatives that work for redirecting to an actual file (in this case they are .jar extensions).

Was it helpful?

Solution

I managed to get it working by removing all the lines it said were sending the Header information early. Here is the code, but if anyone has any suggestions on speeding this up (it is fairly slow) please let me know!

<?php

require_once('simple_html_dom.php');

$html = file_get_html('http://dl.bukkit.org/downloads/craftbukkit/');

$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();
$dom->preserveWhiteSpace = true;

$version = $_GET['version'];

if($version == "beta")
{
    foreach ($dom->getElementsByTagName('div') as $element)
    {
        if($element->getAttribute('class') == "downloadButton chan-beta  mini")
        {
            foreach ($element->getElementsByTagName('a') as $link)
            {
                if( $link->getAttribute('class') == "tooltipd")
                {
                    $url = "Location:http://dl.bukkit.org".$link->getAttribute('href');
                    header($url);
                }
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top