JP19
JP19

Reputation:

php, curl, headers and content-type

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

Upvotes: 2

Views: 7209

Answers (1)

bcosca
bcosca

Reputation: 17555

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;

Upvotes: 3

Related Questions