Php, curl, headers and content-type

I'm having trouble working with curl and the headers returned by the servers.

1) My php file on my_website.com/index.php looks like this (cropped 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 at 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 am at my_website.com/index.php, I should get 404, but this does not happen.

What am I doing wrong?

2) Basically I want to achieve:

my_content_server.com/index.php will decide the content type and send the appropriate headers, and my_website.com/index.php should just send the same content type and other headers (along with the actual data) to the browser. But it looks like my_website.com/index.php is writing custom headers? (Or maybe I don’t understand the job).

Regards, In JP

+2
source share
1 answer

Insert before curl_exec() :

 curl_setopt($ch,CURLOPT_HEADER,true); 

Instead of just echo result, move the headers to the client:

 list($headers,$content) = explode("\r\n\r\n",$result,2); foreach (explode("\r\n",$headers) as $hdr) header($hdr); echo $content; 
+3
source

Source: https://habr.com/ru/post/1394431/


All Articles