file_get_contentsの引数は1つだけではない
今まで普通にfile_get_contents($path)として使っていたけど、いろいろな使い方ができることを初めて知った。もちろんfile_get_contents($url)でHTTP経由でファイルを取得できることは知っていたけれど、相手がBASIC認証を要求するような場合でも対応できるなんて・・・知らなかった。
$username='username';
$password='password';
$option=array(
"http"=>array(
"method"=>"GET",
"header"=>"Authorization: Basic "
. base64_encode($username.":".$password)
)
);
$context=stream_context_create($option);
$result=file_get_contents($url,false,$context);
この例も踏まえて、全部で5つの引数が取れるなんて。ちょっと驚きました。