How do I ignore a moved-header with file_get_contents in PHP?

后端 未结 3 542
无人及你
无人及你 2020-12-03 23:59

I have programmed a simple content-user, that uses file_get_contents, but unfortunately for my IP the site now gives a 302 error that forwards to an image. For all other use

3条回答
  •  遥遥无期
    2020-12-04 00:45

    You need to create a context:

    $context = stream_context_create(
        array (
            'http' => array (
                'follow_location' => false // don't follow redirects
            )
        )
    );
    $html = file_get_contents('http://www.site.net/', false, $context);
    

    See the manual:

    • file_get_contents
    • stream_context_create

    With that said, it's highly likely that there is no content left on the page. It's not impossible to serve a 302 header and serve an HTTP body as well, but it's decidedly unorthodox.

提交回复
热议问题