0

I need "calculate" an URL and redirect to it. I have the following code:

<?php

//Basic HTML parsing with PHP
    include("simple_html_dom.php");

    $htmlCode = file_get_html('http://example.net/example.html');

    foreach($htmlCode->find('div[class=frontPageImage]') as $element) {

        foreach($element->find('img') as $imagee)
            $imgurl = $imagee->src;
    }

header("'Location:".$imgurl."'");

?>

First in $imgurl get the url and later redirect to that url... but it doesn't work..

any idea or suggestion? thanks

2
  • Can you guarantee that the SRC attribute of the images will always contain the full URL? If they're using relative paths, you'll likely have problems. Also, what's your expectation for multiple images? Redirect to the first one? Commented Jul 23, 2012 at 22:18
  • To get the last image, use $imgurl = $htmlCode->find('div[class=frontPageImage] img')->last_child()->src;. Commented Jul 23, 2012 at 22:45

1 Answer 1

3
header("'Location:".$imgurl."'");

Should probably be:

header("Location: " . $imgurl);

I removed the single quote ' because your HTTP header would look like:

'Location: http://site.com/image.jpg'

which is invalid because of the single quotes.

You are also looping and finding many image URL's but only redirecting to the last one.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks drew010, the problem was de quote! And yes I only want redirect the last one. Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.