Returning header as array using Curl

前端 未结 9 1907
再見小時候
再見小時候 2020-12-01 04:22

I\'m trying to get the response & the response headers from CURL using PHP, specifically for Content-Disposition: attachment; so I can return the filename passed within

9条回答
  •  时光取名叫无心
    2020-12-01 05:06

    C.hill's answer is great but breaks when retrieving multiple cookies. Made the change here

    public function get_headers_from_curl_response($response) { 
        $headers = array(); 
        $header_text = substr($response, 0, strpos($response, "\r\n\r\n")); 
        foreach (explode("\r\n", $header_text) as $i => $line) 
             if ($i === 0) $headers['http_code'] = $line; 
             else { 
                  list ($key, $value) = explode(': ', $line); $headers[$key][] = $value; 
             } 
        return $headers; 
    }
    

提交回复
热议问题