60

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:

HTTP/1.1 200 OK
Date: Mon, 28 Feb 2011 14:21:51 GMT
Server: Apache/2.2.14 (Ubuntu)
X-Powered-By: PHP/5.2.12-nmm2
Vary: Accept-Encoding
Content-Length: 6
Content-Type: text/html

6.8320

The actuall response is just 6.8320 as text without any html. I want to retrieve it from the response above by just removing the header information.

I already minified the script a bit:

$url = $_GET['url'];

if ( !$url ) {

  // Passed url not specified.
  $contents = 'ERROR: url not specified';
  $status = array( 'http_code' => 'ERROR' );

} else if ( !preg_match( $valid_url_regex, $url ) ) {

  // Passed url doesn't match $valid_url_regex.
  $contents = 'ERROR: invalid url';
  $status = array( 'http_code' => 'ERROR' );

} else {
  $ch = curl_init( $url );

  if ( strtolower($_SERVER['REQUEST_METHOD']) == 'post' ) {
    curl_setopt( $ch, CURLOPT_POST, true );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $_POST );
  }

  curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
  curl_setopt( $ch, CURLOPT_HEADER, true );
  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

  curl_setopt( $ch, CURLOPT_USERAGENT, $_GET['user_agent'] ? $_GET['user_agent'] : $_SERVER['HTTP_USER_AGENT'] );

  list( $header, $contents ) = preg_split( '/([\r\n][\r\n])\\1/', curl_exec( $ch ), 2 );

  $status = curl_getinfo( $ch );

  curl_close( $ch );
}

// Split header text into an array.
$header_text = preg_split( '/[\r\n]+/', $header );

if ( true ) {
  if ( !$enable_native ) {
    $contents = 'ERROR: invalid mode';
    $status = array( 'http_code' => 'ERROR' );
  }

  // Propagate headers to response.
  foreach ( $header_text as $header ) {
    if ( preg_match( '/^(?:Content-Type|Content-Language|Set-Cookie):/i', $header ) ) {
      header( $header );
    }
  }
  print $contents;
}

Any idea what I need to change to remove the header information from the response?

2
  • 1
    The Content-type header is wrong! Commented Oct 19, 2012 at 7:15
  • 1
    possible to rephase title to mention it is for PHP, I am searching for command line curl Commented Nov 21, 2012 at 7:41

10 Answers 10

104

Just set CURLOPT_HEADER to false.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry sir, how do you do this if you use CURL get command in ssh?
@luka, that would be the -i option or the lack of.
60

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);

2 Comments

this should be the selected answer. You get to keep the details but not have it appear in the result. Much better than not having them if you actually need them (in my case I needed them).
This should definitely be the selected answer. A lot of people want the headers but just don't want it to be part of the content.
6

Update the value of CURLOPT_HEADER to 0 for false

curl_setopt($ch, CURLOPT_HEADER, 0);

1 Comment

This is the Perfect Answer !
3

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.

1 Comment

Quick fix. Thanks
1

If you are using nuSoap, you can access data without headers with $nsoap->responseData or $nsoap->response, if you want the full headers.

Just in case someone needs that.

Comments

1

If for some reason you have to curl_setopt($ch, CURLOPT_HEADER, 1); to get cookies for example, the following worked for me. Not sure if it's 100% reliable but worth a try

$foo = preg_replace('/HTTP(.*)html/s',"",$curlresult);

Comments

1
$content = null;

$ch = curl_init();
$rs = curl_exec($ch);

if (CURLE_OK == curl_errno($ch)) {
  $content = substr($rs, curl_getinfo($ch, CURLINFO_HEADER_SIZE));
}

curl_close($ch);

echo $content;

Comments

0

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)));

Comments

-1

Just don't set CURLOPT_HEADER!

4 Comments

this removes the whole response
Then something else is going on. CURLOPT_HEADER tells CURL to include the header in what's returned from curl_exec. Setting it to false (which is the default anyways) should NOT remove the entire response.
strange for me too. setting to false or removing both result in a blank page.
@RostyslavDzinko if you take the time to look at the accepted answer, you will see that it is the answer and not a comment.
-1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.