PHP

file_get_contentsでPOSTデータ送信

More than 3 years have passed since last update.

    private function __post_ file_get_contents()

{
$url = 'http://qiita.com/';

// POSTデータ
$data = array(
"param1" => "1",
"param2" => 2
);
$data = http_build_query($data, "", "&");

// header
$header = array(
"Content-Type: application/x-www-form-urlencoded",
"Content-Length: ".strlen($data)
);

$context = array(
"http" => array(
"method" => "POST",
"header" => implode("\r\n", $header),
"content" => $data
)
);

$html = file_get_contents($url, false, stream_context_create($context));

return html;
}