-1

url = http://www.homegate.ch/mieten/immobilie-suchen

can someone explain to me the following: when i request the url in my browser, there is no problem (everything works fine). but if i request the url with PHP's file_get_contents, there is a redirect loop:

echo file_get_contents('http://www.homegate.ch/mieten/immobilie-suchen');

I figured out, that this loop is done by JS, but i've no idea how to solve this with PHP. should i use curl instead? but how can i follow this redirection which is done by javascript?

hope you can help me, thanks!

  • `file_get_contents` does not follow Javascript redirects. It can't, because it does not evaluate them. How did you come to that conclusion? – mario Feb 22 '13 at 16:39
  • the problem is, i'm searching for a solution for hours (with curl, zend_http_client etc.) but i always get this annoying redirect loop with this specific URL. no idea how i can handle this properly. i would need the content of the page after this redirection. i also read this article [follow redirects with curl in php](http://stackoverflow.com/questions/4454605/follow-redirects-with-curl-in-php), but it doesn't work... – John Smith Feb 22 '13 at 17:10
  • Please describe the redirect. Is it performed by the website based on preventing scraping perhaps (u-a)? Or does it just occur on your page, after you write it out unfiltered (given your example)? Then filter out the HTML/JS redirect code. – mario Feb 22 '13 at 17:20
  • there is a window.location.reload(true); statement in the sourcecode. i think this will let file_get_contents end up in a redirection. but in the browser, there is no problem with this. so i don't understand the difference... – John Smith Feb 22 '13 at 17:47
  • Last time, file_get_contents doesn't redirect. The browser does if you let that window.location.reload pass unfiltered through the `echo`. – mario Feb 22 '13 at 17:52
  • jep, but i mean when i copy the link directly into my browser everything works fine. if i copy the url of my php file (with file_get_contents in it) in my browser, then it ends up in a redirect loop. – John Smith Feb 22 '13 at 17:55

1 Answers1

1

Instead of file_get_contents() use curl to get a data:

It will represent data as a client,

that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols`

function get_data ($uri) {
    if (!function_exists('curl_init')){ 
    die('Curl is not installed!');
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $uri);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

If didn't solve this problem, this is because Javascript didn't allow to curl to redireting a site: follow redirects with curl in php

Community
  • 1
  • 1
Marin Sagovac
  • 3,932
  • 5
  • 23
  • 53