dongshan8953 2018-11-20 20:11
浏览 233
已采纳

在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!!

  • 写回答

1条回答 默认 最新

  • dsbx40787736 2018-11-20 20:32
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?