我想使用PHP发送POST请求。我需要在内容中配置两个vars。另外,我需要在标头中指定两个cookie。
我尝试用stream_context_create和curl_init失败了。 例如,这是我的stream_context_create代码:
$url = "http://localhost/web/show_comments.php?id=$_GET[com_id]#";
$data = array('body' => "$_GET[comment]", 'userId' => "$_GET[spoof_id]");
$options = array(
'http' => array(
'header' => array("Content-type: application/x-www-form-urlencoded",
"Cookie: user=luis",
"Cookie: password:1234"),
'method' => 'POST',
'content' => http_build_query($data)
)
);
var_dump(http_build_query($data));
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }
var_dump($result);
com_id,spoof_id和comment是GET请求中捕获的参数。
谢谢!
答案 0 :(得分:0)
已解决:即使有两个cookie,也只需配置一个。通过这种方式,解决方案是:
$url = "http://localhost/web/show_comments.php?id=$_GET[com_id]#";
$data = array('body' => "$_GET[comment]", 'userId' => "$_GET[spoof_id]");
$options = array(
'http' => array(
'header' => array("Content-type: application/x-www-form-urlencoded",
"Cookie: user=luis;password:1234"),
'method' => 'POST',
'content' => http_build_query($data)
)
);
var_dump(http_build_query($data));
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }
var_dump($result);