代码之家  ›  专栏  ›  技术社区  ›  Mario

Cookie不会与文件获取内容或CURL请求一起发送

  •  0
  • Mario  · 技术社区  · 7 年前

    file_get_contents 请求具有POST数据和cookie集的URL。

    $postdata = http_build_query(
        array(
            'search' => 'test',
            'token' => '0'
        )
    );
    
    // Create a stream
    $opts = array(
          'http'=> array(
                'method'=>"POST",
                'content' => $postdata,
                'header' => "Content-Type: application/x-www-form-urlencoded\n"."Cookie: session_hash=123456789"
          )
    );
    
    $context = stream_context_create($opts);
    
    // Open the file using the HTTP headers set above
    $file = file_get_contents('https://example.com', false, $context);
    
    echo $file;
    

    这是我看到的数据,但是 Cookie 在请求期间未发送。。。

    我也尝试过使用相同的请求,但是对于CURL,我有同样的问题。

    谢谢!

    1 回复  |  直到 7 年前
        1
  •  1
  •   Barmar    7 年前

    标题可能需要用 \r\n 而不仅仅是 \n . 也可以使用数组,它们将被正确发送。

    'header' => array("Content-Type: application/x-www-form-urlencoded",
                      "Cookie: session_hash=123456789")
    
    推荐文章