troubleshooting Question

How to remove HTTP headers from CURL response? Any idea what I need to change to remove the header information from the response?

Avatar of paga17
paga17Flag for United Arab Emirates asked on
Web DevelopmentPHPScripting Languages
6 Comments1 Solution2897 ViewsLast Modified:
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.

$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?

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros