简体   繁体   中英

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?

Curl cookie is like this: key1=value1; key2=value2; 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM