画面上warnningのメッセージから、file_get_contents()が失敗したことをわかっているが、原因は一体どこからか、単純にwarnningの追跡メッセージからわからなくて、詰まっていた。問題解決次第、以下を纏めました。ちなみに、私はDomain設定の方法で解決しました。
現象:
file_get_contents
//Warnning(Warnning)
file_get_contents('http://localhost/index.php');
//OK
file_get_contents('http://www.google.com/');
対策:
1.php設定チェック:
(先頭に「;」がないことをチェック)
php.ini
extension=php_openssl.dll
allow_url_fopen=On
allow_url_include=Off
2.Domain設定:
以下を追加するだけでOKかもしれませんね。
/etc/hosts
192.168.0.103 domain
3.firewallチェック:
firewallがlocalhostの接続をブロックしているかもしれない、一旦filewallの設定を外して見てみよう。
4.万一...
どうしても動かない場合、ソースの書き方を検討しましょうか。
sample1
//localhostを避けましょう。
//file_get_contents('http://localhost/index.php');
file_get_contents('index.php');
sample2
//代わりに他の関数を使いましょう
function curl_get_contents($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
//※参考から引用だけ、試していない、ご自由にトライしてください。
参考:
・PHP file_get_contents does not work on localhost
---I Love PHP (。・ω・。)ノ♡