Returning header as array using Curl

前端 未结 9 1908
再見小時候
再見小時候 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:17

    Here, this should do it:

    curl_setopt($this->_ch, CURLOPT_URL, $this->_url);
    curl_setopt($this->_ch, CURLOPT_HEADER, 1);
    curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, 1);
    
    $response = curl_exec($this->_ch);
    $info = curl_getinfo($this->_ch);
    
    $headers = get_headers_from_curl_response($response);
    
    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;
    }
    

提交回复
热议问题