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

6
  • 1
    header('HTTP/1.0 404 Not Found - Archive Empty'); exit; Commented Nov 10, 2010 at 6:33
  • Does exit matter if it is the last statement? (I guess I am allowed to echo after sending 404 header?). Commented Nov 10, 2010 at 6:49
  • i guess u trying to return the header from 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 Commented Nov 10, 2010 at 7:23
  • 1
    Yes, you can echo anything after 404. Many nice 404 pages are done in this way. Commented Nov 10, 2010 at 8:08
  • Related: stackoverflow.com/questions/9183178/… Commented Nov 29, 2012 at 14:04

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

7 Comments

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.
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.
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?
If my_website.com/index.php receives a redirect (and other headers), it will also forward that same header to the client.
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.
|

Your Answer

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