简体   繁体   中英

how to get cookie value from curl response in php

I am calling this API by curl:

$url = '/api/auth/login';
$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => "{\"fbUserID\": \"$fbID\", \"fbAccessToken\": \"$Token\"}",
    CURLOPT_HTTPHEADER => array(
        "accept: application/json",
        "cache-control: no-cache",
        "content-type: application/json"
    ),
));

$response = curl_exec($curl);
$jsonresult = json_decode($response);
$err = curl_error($curl);

My question is how can I get the cookie value in response header, I am testing call in POSTMAN API so I am getting cookie value.

Can you please help?

Add this code segment

$cookie=dirname(__FILE__)."/cookie.txt"; 

curl_setopt ($curl, CURLOPT_COOKIEJAR, $cookie);

curl_setopt ($curl, CURLOPT_COOKIEFILE, $cookie);

It will look like this finally

$cookie=dirname(__FILE__)."/cookie.txt"; 
$url = '/api/auth/login'; 
$curl = curl_init(); 
curl_setopt_array($curl, 
array( CURLOPT_URL => $url
, CURLOPT_COOKIEJAR => $cookie
, CURLOPT_COOKIEFILE => $cookie
, CURLOPT_RETURNTRANSFER => true
, CURLOPT_ENCODING => ""
, CURLOPT_MAXREDIRS => 10
, CURLOPT_TIMEOUT => 30
, CURLOPT_CUSTOMREQUEST => "POST"
, CURLOPT_POSTFIELDS => "{\"fbUserID\": \"$fbID\", \"fbAccessToken\": \"$Token\"}"
,CURLOPT_HTTPHEADER => array(
"accept: application/json",
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$jsonresult = json_decode($response);
$err = curl_error($curl);

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