在PHP中使用Cookie进行POST

I want to send a POST request with PHP. I need to configure in the content two vars . Also I need to specify two cookies in header.

I tried with stream_context_create and with curl_init without success. For example, here is my stream_context_create code:

    $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 and comment are parameters catched in a GET request.

Thanks!!

doyrte8419
doyrte8419 已解决:即使有两个cookie,也只需配置一个。通过这种方式,解决方案是:“Cookie:user=luis;密码:1234”)
接近 2 年之前 回复

1个回答

SOLVED: Even if there are two cookies, only one have to be configured. By this way, the solution is:

    $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);
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐