我在处理服务器返回的 curl 和 header 时遇到了一些问题。
1) 我在 my_website.com/index.php 上的 php 文件看起来像这样(修剪过的版本):
<?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;
?>
my_content_server.com/index.php 上的 php 文件如下所示:
<?php
header("HTTP/1.0 404 Not Found - Archive Empty");
echo "Some content > 600 words to make chrome/IE happy......";
?>
我希望当我访问 my_website.com/index.php 时,我应该得到 404,但这并没有发生。
我究竟做错了什么?
2)基本上我想要实现的是:
my_content_server.com/index.php 将决定内容类型并发送适当的标题,而 my_website.com/index.php 应该只将相同的内容类型和其他标题(以及实际数据)发送到浏览器。但似乎 my_website.com/index.php 正在编写自己的标题? (或者也许我没有正确理解工作)。
问候,
J.P
最佳答案
插入前 curl_exec()
:
curl_setopt($ch,CURLOPT_HEADER,true);
而不仅仅是
echo
根据结果,将 header 也转发给客户端:list($headers,$content) = explode("\r\n\r\n",$result,2);
foreach (explode("\r\n",$headers) as $hdr)
header($hdr);
echo $content;
关于php、curl、标题和内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4141750/