LoginSignup
8
8

More than 5 years have passed since last update.

file_get_contents() で HTTP経由でデータを取得しようとしてエラーになったとき

Last updated at Posted at 2015-02-02

php.ini の allow_url_fopen というオプションに関するメモ。


PHP の file_get_contents() でデータを取得する、

<?php
$contents = file_get_contents('http://example.com/');

ごくごく普通のコードです。
ごくごく普通に動作する。

と思っていたら、一部のサーバでだけ、常に false が返ってくる。データが取れない。なぜ。

ステータスコードなどを見るために $http_response_header を確認してみると

<?php
$contents = file_get_contents('http://example.com/');
var_dump($http_response_header);

結果は NULL……

大半のサーバでは動いているので、なんかのサーバ設定だろうと調べた結果、php.ini の allow_url_fopen というオプションというものにたどり着く。このオプション初めて知った。

PHP: 実行時設定 - Manual
http://php.net/manual/ja/filesystem.configuration.php#ini.allow-url-fopen

allow_url_fopen「ftpまたはhttpプロトコルを用いて リモートファイルにアクセスするために提供されています」 とのことで、on がデフォルト。それが件のサーバでは off になっていた。

.htaccess に以下を追加することで、無事に動作するように。

php_flag allow_url_fopen on

知らないとハマりますね…

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8