PHP file_get_contents () behaves differently in browser

I am trying to load the contents of a webpage using PHP. When I issue the command:

$f = file_get_contents("http://mobile.mybustracker.co.uk/mobile.php?searchMode=2");

It returns a page stating that the server is down. However, when I paste the same URL into my browser, I get the expected page.

Can anyone understand what causes this? Does file_get_contents pass any headers that distinguishes it from a browser request?

+5
source share
2 answers

Yes, there are differences - the browser, as I would say, sends a lot of additional HTTP headers ; and those sent by both probably don't have the same meaning.

, , , HTTP- Accept.

, file_get_contents, :

$opts = array('http' =>
    array(
        'method'  => 'GET',
        //'user_agent '  => "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2) Gecko/20100301 Ubuntu/9.10 (karmic) Firefox/3.6",
        'header' => array(
            'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*\/*;q=0.8
'
        ), 
    )
);
$context  = stream_context_create($opts);

$f = file_get_contents("http://mobile.mybustracker.co.uk/mobile.php?searchMode=2", false, $context);
echo $f;

HTML- .


:

  • User-Agent, ,
  • Accept - , Firefox, Firefox, file_get_contents.
    • , , , .


:

+16

%20

-3

All Articles