Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove HTTP headers from CURL response?

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?

like image 851
UpCat Avatar asked Feb 28 '11 13:02

UpCat


People also ask

How do I get rid of HTTP headers?

Open the site which you would like to open and then click on the HTTP Response Headers option. Click on the X-Powered-By header and then click Remove on the Actions Pane to remove it from the response.

How do I pass HTTP headers in curl?

To send an HTTP header with a Curl request, you can use the -H command-line option and pass the header name and value in "Key: Value" format. If you do not provide a value for the header, this will remove the standard header that Curl would otherwise send. The number of HTTP headers is unlimited.

How do I create a custom header in curl?

To add a custom header to Curl, you need to pass the -H "header: value" command line parameter to the Curl request. In this Curl Custom Headers example, we are sending a custom header to the ReqBin echo URL.

What is curl in response?

'cURL' is a command-line tool that lets you transmit HTTP requests and receive responses from the command line or a shell script. It is available for Linux distributions, Mac OS X, and Windows. To use cURL to run your REST web API call, use the cURL command syntax to construct the command.


1 Answers

Just set CURLOPT_HEADER to false.

like image 130
Ewan Heming Avatar answered Oct 14 '22 16:10

Ewan Heming