使用
file_get_contents发送HTTP POST请求不是那么难,实际上:正如你猜到的,你必须使用$ context参数。
PHP手册中有一个例子,在这个页面:HTTP context options(引用):
$postdata = http_build_query(
array(
'var1' => 'some content',
'var2' => 'doh'
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://example.com/submit.php', false, $context);
基本上,你必须创建一个流,有正确的选项(在该页上有一个完整的列表),并使用它作为file_get_contents的第三个参数 – 没有更多;-)
作为sidenote:一般来说,发送HTTP POST请求,我们倾向于使用curl,它提供了很多选项一个 – 但流是一个好的东西PHP没有人知道…太糟糕了。 。