Returning header as array using Curl

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

    Another my implementation:

    function getHeaders($response){
    
        if (!preg_match_all('/([A-Za-z\-]{1,})\:(.*)\\r/', $response, $matches) 
                || !isset($matches[1], $matches[2])){
            return false;
        }
    
        $headers = [];
    
        foreach ($matches[1] as $index => $key){
            $headers[$key] = $matches[2][$index];
        }
    
        return $headers;
    }
    

    Used in case, which request format is:

    Host: *
    Accept: *
    Content-Length: *
    and etc ...

提交回复
热议问题