使い方
// GETの場合、パラメータはURLに含めて第2引数は無しにする
$cont = fileGetContents("http://example.com/get/?foo=bar&hoge=fuga");
// POSTの場合、パラメータを第2引数に配列で渡す
$cont = fileGetContents("http://example.com/get/", array("foo" => "bar", "hoge" => "fuga");
定義
// ユーザエージェントとリファラは固定(;´Д`)
function fileGetContents($url, $post = array()) {
$header = array(
"User-Agent: Example-Browser/2.0",
"Referer: http://www.example.com/"
);
if ($post) {
$data = http_build_query($post);
array_push($header, "Content-Type: application/x-www-form-urlencoded");
array_push($header, "Content-Length: " . strlen($data));
$options = array(
"http" => array(
"method" => "POST",
"header" => implode("\r\n", $header),
"content" => $data
)
);
} else {
$options = array(
"http" => array(
"method" => "GET",
"header" => implode("\r\n", $header)
)
);
}
return file_get_contents($url, false, stream_context_create($options));
}