Link to home
Start Free TrialLog in
Avatar of paga17
paga17Flag for United Arab Emirates

asked on

How to remove HTTP headers from CURL response? Any idea what I need to change to remove the header information from the 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
Server: nginx/1.0.0
Date: Tue, 14 Feb 2012 14:38:46 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.3.8

<chart><chart_data><row><null/><string>Cash On Delivery</string><string>Credit Card Swipe</string><string>Credit Card Online</string></row><row><string></string><number>3</number><number>3</number><number>5</number></row></chart_data><chart_label shadow='low' alpha='65' size='10' position='inside' as_percentage='true' /><chart_pref select='true' drag='false' rotation_x='50' /><chart_rect x='70' y='50' width='300' height='180' positive_alpha='0' /><chart_type>3d pie</chart_type><draw><rect bevel='bg' layer='background' x='0' y='0' width='480' height='300' fill_color='ffffff' line_thickness='0' /><text layer='background' shadow='high' color='' alpha='10' size='97' x='-3' y='205' width='500' height='150'></text><text shadow='low' color='' alpha='10' rotation='-90' size='30' x='-1' y='235' width='300' height='50' >gridzDIRECT </text></draw><filter><shadow id='high' distance='3' angle='45' alpha='50' blurX='10' blurY='10' /><shadow id='low' distance='2' angle='45' alpha='60' blurX='10' blurY='10' /><bevel id='bg' angle='90' blurX='0' blurY='200' distance='50' highlightAlpha='15' shadowAlpha='15' type='inner' /></filter><legend shadow='low' layout='horizontal' margin='20' x='65' y='-10' width='345' height='25' fill_alpha='0' color='000000' alpha='75' /><context_menu full_screen='false' /></chart>

The actuall response is just the XML.want to retrieve it from the response above by just removing the header information.

Open in new window


$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?
Avatar of eriksmtka
eriksmtka
Flag of United States of America image

The line:
curl_setopt( $ch, CURLOPT_HEADER, true );

Open in new window


can probably be removed or changed to:
curl_setopt( $ch, CURLOPT_HEADER, false);

Open in new window

Avatar of paga17

ASKER

Hi, When i changed it to curl_setopt( $ch, CURLOPT_HEADER, false); I see only a blank page.
Looks like the script separates headers from content after curl_exec, so change that line back.

You might be able to quell the headers by commenting out the line:

header( $header );

Open in new window


and while you're at it the 2 lines above it and the 2 lines below it.  This is going to change what this script does and might have reprecussions elsewhere, so it's probably a 98% solution.

The questions I have are:  Did you write this script?  If not, what does it say it's supposed to do?  And, what are you trying to achieve?
Avatar of paga17

ASKER

Hi Erik,

I have a dashboard running on the localhost. I have a pie chart running on the dashboard. The data for the pie chart is from the remote server which is an XML file through JSON.  If you could guide me how to do this in a simple way which would be very much appreciated.

regards,
i still don't understand what you're trying to acheive.  your question makes it sound like you have a lot more moving pieces than the code you posted does.

did you change this line back to true?
curl_setopt( $ch, CURLOPT_HEADER, true );

Open in new window


and did you comment out these lines?
/* 
foreach ( $header_text as $header ) {
    if ( preg_match( '/^(?:Content-Type|Content-Language|Set-Cookie):/i', $header ) ) {
      header( $header );
    }
} 
*/

Open in new window


if so, what were the results?  did it remove the headers you're seeing from the output?
ASKER CERTIFIED SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial