How to remove HTTP headers from CURL response?

前端 未结 10 1141
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 17:26

I have a php script that returns just plain text without any html. Now I want to make a cURL request to that script and I get the following response:

<
相关标签:
10条回答
  • 2020-12-13 17:29

    Just for a later use if anyone else needs. I was into same situation, but just need to remove header text, not content. The response i was getting in the header was (including white space):

    HTTP/1.1 200 OK
    Cache-Control: private, no-cache, no-store, must-revalidate
    Content-Language: en
    Content-Type: text/html
    Date: Tue, 25 Feb 2014 20:59:29 GMT
    Expires: Sat, 01 Jan 2000 00:00:00 GMT
    Pragma: no-cache
    Server: nginx
    Vary: Cookie, Accept-Language, Accept-Encoding
    transfer-encoding: chunked
    Connection: keep-alive
    

    I wanted to remove starting from HTTP till keep-alive with white space:

    $contents = preg_replace('/HTTP(.*)alive/s',"",$contents);
    

    that did for me.

    0 讨论(0)
  • 2020-12-13 17:35

    If someone already saved the curl response to a file (like me) and therefore don't know how big the header was to use substr, try:

    $file = '/path/to/file/with/headers';
    file_put_contents($file, preg_replace('~.*\r\n\r\n~s', '', file_get_contents($file)));
    
    0 讨论(0)
  • 2020-12-13 17:39

    Update the value of CURLOPT_HEADER to 0 for false

    curl_setopt($ch, CURLOPT_HEADER, 0);
    
    0 讨论(0)
  • 2020-12-13 17:41

    Make sure you put set the header flag:

            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HEADER, true );
            curl_setopt($ch, CURLOPT_TIMEOUT, Constants::HTTP_TIMEOUT);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, Constants::HTTP_TIMEOUT);
            $response = curl_exec($ch); 
    

    Do this after your curl call:

    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $headerstring = substr($response, 0, $header_size);
    $body = substr($response, $header_size);
    

    EDIT: If you'd like to have header in assoc array, add something like this:

        $headerArr = explode(PHP_EOL, $headerstring);
        foreach ($headerArr as $headerRow) {
            preg_match('/([a-zA-Z\-]+):\s(.+)$/',$headerRow, $matches);
            if (!isset($matches[0])) {
                continue;
            }
            $header[$matches[1]] = $matches[2];
        }
    

    Result print_r($header):

    (
        [content-type] => application/json
        [content-length] => 2848
        [date] => Tue, 06 Oct 2020 10:29:33 GMT
        [last-modified] => Tue, 06 Oct 2020 10:17:17 GMT
    )
    

    Don't forget to close connection curl_close($ch);

    0 讨论(0)
  • 2020-12-13 17:42

    Just set CURLOPT_HEADER to false.

    0 讨论(0)
  • 2020-12-13 17:44

    Just do not set the curl_header in the curl request or set it to z or false
    like this
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_HEADER, false);

    0 讨论(0)
提交回复
热议问题