how to get the cookies from a php curl into a variable

后端 未结 8 833
囚心锁ツ
囚心锁ツ 2020-11-22 15:08

So some guy at some other company thought it would be awesome if instead of using soap or xml-rpc or rest or any other reasonable communication protocol he just embedded all

8条回答
  •  眼角桃花
    2020-11-22 15:49

    This does it without regexps, but requires the PECL HTTP extension.

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    $result = curl_exec($ch);
    curl_close($ch);
    
    $headers = http_parse_headers($result);
    $cookobjs = Array();
    foreach($headers AS $k => $v){
        if (strtolower($k)=="set-cookie"){
            foreach($v AS $k2 => $v2){
                $cookobjs[] = http_parse_cookie($v2);
            }
        }
    }
    
    $cookies = Array();
    foreach($cookobjs AS $row){
        $cookies[] = $row->cookies;
    }
    
    $tmp = Array();
    // sort k=>v format
    foreach($cookies AS $v){
        foreach ($v  AS $k1 => $v1){
            $tmp[$k1]=$v1;
        }
    }
    
    $cookies = $tmp;
    print_r($cookies);
    

提交回复
热议问题