Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set the cookie content with CURL?

Tags:

php

curl

cookies

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. What I want is, to write the data for the cookie myself, so CURL uses it.

like image 781
Ahatius Avatar asked May 13 '12 08:05

Ahatius


People also ask

How do you set the cookie value in curl?

Cookies are passed to Curl with the --cookie "Name=Value" command line parameter. Curl automatically converts the given parameter into the Cookie: Name=Value request header. Cookies can be sent by any HTTP method, including GET, POST, PUT, and DELETE, and with any data, including JSON, web forms, and file uploads.

Does curl handle cookies?

curl has a full cookie "engine" built in. If you just activate it, you can have curl receive and send cookies exactly as mandated in the specs. tell curl a file to read cookies from and start the cookie engine, or if it is not a file it will pass on the given string.

How do I change the content type in curl?

To send the Content-Type header using Curl, you need to use the -H command-line option. For example, you can use the -H "Content-Type: application/json" command-line parameter for JSON data. Data is passed to Curl using the -d command-line option.

What is curl cookie?

curl can specify and store the cookies encountered during HTTP operations. The -cookie COOKIE_IDENTIFER option specifies which cookies to provide. Cookies are defined as name=value . Multiple cookies should be delimited with a semicolon ( ; ): $ curl http://example.com --cookie "user=username;pass=hack"


2 Answers

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");
like image 109
vvondra Avatar answered Oct 02 '22 16:10

vvondra


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.

like image 42
Mahmoud Al-Qudsi Avatar answered Oct 02 '22 17:10

Mahmoud Al-Qudsi