Is it possible to set the cookie content with CURL?

后端 未结 3 1489
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 11:16

I have been searching for a way, to specify the cookie data for CURL. I have found some solutions on how to save the cookies from a visited page, but that\'s not what I need

相关标签:
3条回答
  • 2020-11-30 11:47

    cURL can use a cookie file in Netscape format. Just create such a file yourself and use as the CURLOPT_COOKIEFILE option.

    0 讨论(0)
  • 2020-11-30 11:48

    You can use curl_setopt with the CURLOPT_COOKIE constant:

    <?php
    // create a new cURL resource
    $ch = curl_init();
    
    // cookies to be sent
    curl_setopt($ch, CURLOPT_COOKIE, "fruit=apple; colour=red");
    
    0 讨论(0)
  • 2020-11-30 11:51

    You really should read the documentation - it's listed with exactly the keywords you'd expect and contains a lot of helpful info:

    -b, --cookie

    (HTTP) Pass the data to the HTTP server as a cookie. It is supposedly the data previously received from the server in a "Set-Cookie:" line. The data should be in the format "NAME1=VALUE1; NAME2=VALUE2".

    If no '=' symbol is used in the line, it is treated as a filename to use to read previously stored cookie lines from, which should be used in this session if they match. Using this method also activates the "cookie parser" which will make curl record incoming cookies too, which may be handy if you're using this in combination with the -L, --location option. The file format of the file to read cookies from should be plain HTTP headers or the Netscape/Mozilla cookie file format.

    NOTE that the file specified with -b, --cookie is only used as input. No cookies will be stored in the file. To store cookies, use the -c, --cookie-jar option or you could even save the HTTP headers to a file using -D, --dump-header!

    If this option is set more than once, the last one will be the one that's used.

    0 讨论(0)
提交回复
热议问题