Download the contents of a URL in PHP even if it returns a 404

前端 未结 4 1041
半阙折子戏
半阙折子戏 2020-12-05 11:06

I want to download the contents of a URL using PHP, even if the HTTP response code is 404. file_get_contents will error out, and I wasn\'t able to find an answe

4条回答
  •  余生分开走
    2020-12-05 11:52

    By default file_get_contents only returns the content of HTTP 200 responses.

    With curl you get the headers and the content separately.

    As of PHP 5.0, you can also specify a context for file_get_contents, allowing you to do it without relying on url (See Gordon's answer).

提交回复
热议问题