Post 如何使用PHP header()函数发布到页面?
我在上面找到了以下代码,我认为它符合我的要求,但不起作用:Post 如何使用PHP header()函数发布到页面?,post,header,http-headers,Post,Header,Http Headers,我在上面找到了以下代码,我认为它符合我的要求,但不起作用: $host = "www.example.com"; $path = "/path/to/script.php"; $data = "data1=value1&data2=value2"; $data = urlencode($data); header("POST $path HTTP/1.1\r\n"); header("Host: $host\r\n"); header("Content-type: applicatio
$host = "www.example.com";
$path = "/path/to/script.php";
$data = "data1=value1&data2=value2";
$data = urlencode($data);
header("POST $path HTTP/1.1\r\n");
header("Host: $host\r\n");
header("Content-type: application/x-www-form-urlencoded\r\n");
header("Content-length: " . strlen($data) . "\r\n");
header("Connection: close\r\n\r\n");
header($data);
我希望在不将用户发送到中间页面,然后使用JavaScript重定向他们的情况下发布表单数据。我也不想使用GET,所以使用back按钮就不那么容易了
这个代码有什么问题吗?还是有更好的方法
编辑我正在考虑header函数的作用。我想我可以让浏览器把数据发回到服务器上,但这不是它的本意。相反,我在代码中找到了一种完全不需要post的方法(不是中断,而是继续切换到下一个案例)。header函数用于将HTTP响应头发送回用户(即,您不能使用它创建请求头) 我可以问一下为什么要这样做吗?为什么要模拟POST请求,而此时您正好可以在那里,然后以某种方式对数据进行操作?我当然假设script.php驻留在您的服务器上
要创建POST请求,请使用fsockopen()打开到主机的TCP连接,然后对从fsockopen()返回的处理程序使用fwrite()使用与OP中的头函数相同的值。或者,您可以使用cURL。除了Salaryman所说的之外,看看中的类,那里有HTTP请求类,即使您的PHP发行版中没有安装cURL扩展,您也可以使用这些类
private function sendHttpRequest($host, $path, $query, $port=80){
header("POST $path HTTP/1.1\r\n" );
header("Host: $host\r\n" );
header("Content-type: application/x-www-form-urlencoded\r\n" );
header("Content-length: " . strlen($query) . "\r\n" );
header("Connection: close\r\n\r\n" );
header($query);
}
这将立即为您提供有一个很好的类可以满足您的需要。它可以在以下位置下载:今天非常需要这个问题的答案,因为不是每个人都想使用cURL来使用web服务。PHP也允许使用以下代码实现这一点
function get_info()
{
$post_data = array(
'test' => 'foobar',
'okay' => 'yes',
'number' => 2
);
// Send a request to example.com
$result = $this->post_request('http://www.example.com/', $post_data);
if ($result['status'] == 'ok'){
// Print headers
echo $result['header'];
echo '<hr />';
// print the result of the whole request:
echo $result['content'];
}
else {
echo 'A error occured: ' . $result['error'];
}
}
function post_request($url, $data, $referer='') {
// Convert the data array into URL Parameters like a=b&foo=bar etc.
$data = http_build_query($data);
// parse the given URL
$url = parse_url($url);
if ($url['scheme'] != 'http') {
die('Error: Only HTTP request are supported !');
}
// extract host and path:
$host = $url['host'];
$path = $url['path'];
// open a socket connection on port 80 - timeout: 30 sec
$fp = fsockopen($host, 80, $errno, $errstr, 30);
if ($fp){
// send the request headers:
fputs($fp, "POST $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
if ($referer != '')
fputs($fp, "Referer: $referer\r\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-length: ". strlen($data) ."\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $data);
$result = '';
while(!feof($fp)) {
// receive the results of the request
$result .= fgets($fp, 128);
}
}
else {
return array(
'status' => 'err',
'error' => "$errstr ($errno)"
);
}
// close the socket connection:
fclose($fp);
// split the result header from the content
$result = explode("\r\n\r\n", $result, 2);
$header = isset($result[0]) ? $result[0] : '';
$content = isset($result[1]) ? $result[1] : '';
// return as structured array:
return array(
'status' => 'ok',
'header' => $header,
'content' => $content);
}
函数get_info()
{
$post_数据=数组(
“测试”=>“foobar”,
“好的”=>“是的”,
“数字”=>2
);
//向example.com发送请求
$result=$this->post_请求('http://www.example.com/“,$post_数据);
如果($result['status']=='ok'){
//打印标题
echo$result['header'];
回声“”; //打印整个请求的结果: echo$result['content']; } 否则{ 回显“发生错误:”。$result['error']; } } 函数post_请求($url、$data、$referer=''){ //将数据数组转换为URL参数,如a=b&foo=bar等。 $data=http\u build\u query($data); //解析给定的URL $url=解析url($url); 如果($url['scheme']!='http'){ die('错误:仅支持HTTP请求!'); } //提取主机和路径: $host=$url['host']; $path=$url['path']; //在端口80上打开套接字连接-超时:30秒 $fp=fsockopen($host,80,$errno,$errstr,30); 如果($fp){ //发送请求头: fputs($fp,“POST$path HTTP/1.1\r\n”); fputs($fp,“主机:$Host\r\n”); 如果($referer!='') fputs($fp,“Referer:$Referer\r\n”); fputs($fp,“内容类型:application/x-www-form-urlencoded\r\n”); fputs($fp,“内容长度:”.strlen($data)。“\r\n”); fputs($fp,“连接:关闭\r\n\r\n”); FPUT($fp$data); $result=''; 而(!feof($fp)){ //接收请求的结果 $result.=fgets($fp,128); } } 否则{ 返回数组( “状态”=>“错误”, 'error'=>“$errstr($errno)” ); } //关闭插座连接: fclose($fp); //从内容中拆分结果标题 $result=explode(“\r\n\r\n”,$result,2); $header=isset($result[0])?$result[0]:“”; $content=isset($result[1])?$result[1]:“”; //作为结构化数组返回: 返回数组( “状态”=>“正常”, 'header'=>$header, “内容”=>$content); }
Thxs,我有点困惑,是的,你是对的,在脚本中有一个更好的方法,不需要发布。这对我不起作用-我得到了一个(无信息的)我一添加代码就出现内部服务器错误。为什么参数签名中有一个未使用的
$port