PHP POST具有header()和错误的标题问题 [英] PHP POST with header() and Bad header problems

查看:55
本文介绍了PHP POST具有header()和错误的标题问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中发送标头时遇到了一个令人震惊的问题.我已经在SO和其他网站上花费了大约45分钟的时间,但我无法提出合理的理由来解决我的问题.

I've come across a dumbfounding issue with sending headers in PHP. I've spent about 45 minutes reading on SO and other sites and I can't come up with a legitimate reason for my problem.

我需要向另一个服务器发送POST请求,并且正在使用PHP header()函数设置值.我下面有示例代码.

I need to send a POST request to another server and I'm using the PHP header() function to set the values. I have sample code below.

    $server = 'http://fakedomain.com';
    $server_path = '/';
    $request = 'key=value&key2=value2';
    header("POST $server_path HTTP/1.1" );
    header("Host: $server\r\n" );
    header("Content-type: application/x-www-form-urlencoded\r\n" );
    header("Content-length: ".strlen($request)."\r\n" );
    header("Connection: close\r\n\r\n" );
    header($request);

我尝试了各种选项,但是每个选项都会在我的日志文件中导致相同的错误

I've tried a variety of options but each of them results in the same error in my log file

malformed header from script. Bad header=POST / HTTP/1.1: php5.cgi

我是一位经验丰富的PHP程序员,他不需要手动发送HTTP发布请求.

I'm an experience PHP programmer who just hasn't had much need to manually send HTTP post requests.

我希望代码重定向浏览器,所以这就是我决定使用此方法的原因.

I want the code to redirect the browser so that's why I decided to use this method.

我做对了吗?

还有其他一些标准方法,我只是不知道吗?

推荐答案

header()发送响应标题.

header() sends a response header.

听起来您想在后端发出请求.

It sounds like you want to make a request, on the back end.

因此,您可能想使用 curl 发出请求.

So you probably want to use curl to make the request.

如果处理完响应后,您想将某种标头发送给用户代理(浏览器),则header()将是合适的.

If when you're done processing the response, you want to send some kind of header to the user agent (browser), then header() would be appropriate.

这篇关于PHP POST具有header()和错误的标题问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆