PHPのfile_get_contentsでhttpsのサイトが読み込めない

更新:

Windows上でPHPを実行したときの話。

file_get_contents()関数は、ローカルのファイルだけでなく、以下のように他のサイトのデータも取得できる。

$data = file_get_contents('https://example.com/');

しかし、以下のようなエラーが表示された。

PHP Warning:  file_get_contents(): Unable to find the wrapper "https" - did you forget to enable it when you configured PHP? in D:\example\test.php on line 34
PHP Warning:  file_get_contents(https://example.com/): failed to open stream: No such file or directory in D:\example\test.php on line 34

レンタルサーバーでは各種拡張モジュールがあらかじめ有効になっているが、Windows版PHPをただZIPを展開して使用している場合、基本的にそれらが無効になっている。

そこで、以下のように php.ini の「Dynamic Extensions」と書かれたセクションにあるOpenSSLモジュールを有効にする必要がある。 セミコロン(;)でコメントアウトされているので、それを外す。

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;

(中略)

extension=openssl

以下のコマンドでロードされているモジュールが確認できる。

C:\Users\ユーザー名>php -r print_r(get_loaded_extensions());

Array
(
    [0] => Core
    [1] => bcmath
    [2] => calendar
    [3] => ctype
    [4] => date
    [5] => filter
    [6] => hash
    [7] => iconv
    [8] => json
    [9] => SPL
    [10] => pcre
    [11] => readline
    [12] => Reflection
    [13] => session
    [14] => standard
    [15] => mysqlnd
    [16] => tokenizer
    [17] => zip
    [18] => zlib
    [19] => libxml
    [20] => dom
    [21] => PDO
    [22] => openssl
    [23] => SimpleXML
    [24] => xml
    [25] => xmlreader
    [26] => xmlwriter
    [27] => mbstring
    [28] => Phar
)

本件はfile_get_contentsだけでなく、getimagesizeなど他のサイトのデータを読み込めるすべての関数で起こり得る。


このエントリーをはてなブックマークに追加