FunnyCheese
FunnyCheese

Reputation: 341

Curl set cookie to header from json

I have this

{
  "session":"59a28f4741b0800302147c4e8db00e5e",
  "id":"765611988531745",
  "rememberLogin":"76561198852231745||67b583c48e95a76fbcf7da254714e206"
}

how can I set this cookie to curl headers when i send the POST request?

Upvotes: 2

Views: 3274

Answers (1)

Ugur Eren
Ugur Eren

Reputation: 2204

Curl cookie is like this: key1=value1; key2=value2;

So you need to convert your json to that. You can use this simple function to do that.

function jsontocookie($json) {
    $ret = "";

    foreach(json_decode($json, true) as $key => $value){
        $ret .= $key."=".$value."; ";
    }

    return $ret;
}

And send with CURLOPT_COOKIE

curl_setopt($ch, CURLOPT_COOKIE, jsontocookie($yourJsonData));

Upvotes: 4

Related Questions