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

报告相同问题?

悬赏问题

  • ¥20 wpf datagrid单元闪烁效果失灵
  • ¥15 券商软件上市公司信息获取问题
  • ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout
  • ¥15 Android studio AVD启动不了
  • ¥15 陆空双模式无人机怎么做
  • ¥15 想咨询点问题,与算法转换,负荷预测,数字孪生有关
  • ¥15 C#中的编译平台的区别影响
  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)