发布日期:2018-03-26
如何使用PHP发送POST请求(使用file_get_contents来发送POST请求)?+ 查看更多
如何使用PHP发送POST请求(使用file_get_contents来发送POST请求)?
+ 查看更多
发布日期:2017-04-07 10:26
分类:PHP
浏览次数:228
事实上我想要在查询操作完成后读取内容。问题是URL只接收POST方法,用GET方法没有响应。。。
我必须在domdocument或者file_get_contents()的帮助下读取所有内容,有没有方法让我用POST方法发送参数,然后通过PHP读取内容?
回答
PHP5下的非CURL方法:
$url = 'http://server.com/path';
$data = array('key1' => 'value1', 'key2' => 'value2');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }
var_dump($result);
有关添加报文头方法的更多信息,请参阅PHP手册,如下:
stream_context_create: http://php.net/manual/en/function.stream-context-create.php