2

I am having some trouble working with curl and headers returned by servers.

1) My php file on my_website.com/index.php looks like this (trimmed version):

<?php

$url = 'http://my_content_server.com/index.php';

//Open connection
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);

echo $result;
?>

The php file on my_content_server.com/index.php looks like this:

<?php
header("HTTP/1.0 404 Not Found - Archive Empty");
echo "Some content > 600 words to make chrome/IE happy......";
?>

I expect that when I visit my_website.com/index.php, I should get a 404, but that is not happening.

What am I doing wrong?

2) Basically what I want to achieve is:

my_content_server.com/index.php will decide the content type and send appropriate headers, and my_website.com/index.php should just send the same content-type and other headers (along with actual data) to the browser. But it seems that my_website.com/index.php is writing its own headers? (Or maybe I am not understanding the working correctly).

regards, JP

  • 1
    `header('HTTP/1.0 404 Not Found - Archive Empty'); exit;` – ajreal Nov 10 '10 at 06:33
  • Does exit matter if it is the last statement? (I guess I am allowed to echo after sending 404 header?). –  Nov 10 '10 at 06:49
  • i guess u trying to return the header from http://my_content_server.com/index.php? `... ob_start(); $header = obj_get_contents()// your header; ob_end_clean(); header($header); echo "additional message"; ` and use the `curl_setopt($ch,CURLOPT_HEADER,true);` suggested by @stillstanding – ajreal Nov 10 '10 at 07:23
  • 1
    Yes, you can echo anything after 404. Many nice 404 pages are done in this way. – Halil Özgür Nov 10 '10 at 08:08
  • Related: http://stackoverflow.com/questions/9183178/php-curl-retrieving-response-headers-and-body-in-a-single-request – Maxime Pacary Nov 29 '12 at 14:04
  • You should set the response code using http://php.net/manual/en/function.http-response-code.php – Geoffrey Aug 13 '17 at 04:07

1 Answers1

3

Insert before curl_exec():

curl_setopt($ch,CURLOPT_HEADER,true);

Instead of just echo'ing the result, forward the headers to the client as well:

list($headers,$content) = explode("\r\n\r\n",$result,2);
foreach (explode("\r\n",$headers) as $hdr)
    header($hdr);
echo $content;
bcosca
  • 17,371
  • 5
  • 40
  • 51
  • This does have an affect, however, not what I desired. After setting this flag, the header is displayed verbatim by my_website.com/index.php. HTTP/1.0 404 Not Found - Archive Empty Date: Wed, 10 Nov 2010 08:09:30 GMT Server: Apache/2.2.11 (Ubuntu) PHP/5.2.6-3ubuntu4.6 with Suhosin-Patch mod_ssl/2.2.11 OpenSSL/0.9.8g X-Powered-By: PHP/5.2.6-3ubuntu4.6 Vary: Accept-Encoding Content-Length: 282 Connection: close Content-Type: text/html. I client browses still does not receive 404 error. –  Nov 10 '10 at 08:11
  • Thanks for the tip. This has some error which I am unable to trace. I turned display_errors on, and get this: Notice: Undefined offset: 1 in /var/www/index.php on line 42 Warning: Header may not contain more than a single header, new line detected. in /var/www/index.php on line 44. 42 line is the "explode" line and 44 the header($hdr) one. –  Nov 10 '10 at 08:38
  • Whoa. Got it. '\r\n\r\n' and '\r\n' should be "\r\n\r\n" and "\r\n". Works like a charm. Btw, can there be issues with redirects or "continue" headers or keep alive connections with this approach? –  Nov 10 '10 at 08:47
  • If my_website.com/index.php receives a redirect (and other headers), it will also forward that same header to the client. – bcosca Nov 10 '10 at 08:50
  • As far as the content server is concerned, the cURL client is just another HTTP client. And the connection is not pass-thru. You now have 2 different HTTP connections on the cURL client side - one to the content server, another to the end-user. So it's acting as a proxy. You may want to filter the headers if that's not the behavior you expect. – bcosca Nov 10 '10 at 09:03
  • Thanks stillstanding for solving the problem. –  Nov 10 '10 at 09:04
  • You may want to filter some of the headers. For example "Transfer-Encoding:" may lead to problems if you just pass it through! – OderWat Oct 24 '12 at 15:46