如何使用PHP header()函数POST到页面?

Dar*_*ein 20 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: application/x-www-form-urlencoded\r\n");
header("Content-length: " . strlen($data) . "\r\n");
header("Connection: close\r\n\r\n");
header($data);
Run Code Online (Sandbox Code Playgroud)

我希望发布表单数据而不将用户发送到中间页面,然后使用JavaScript重定向它们.我也不想使用GET,因此使用后退按钮并不容易.

这段代码有问题吗?还是有更好的方法?

编辑我在考虑标题函数会做什么.我以为我可以让浏览器用数据回发到服务器,但这不是它的意思.相反,我在我的代码中找到了一种方法来避免需要一个帖子(不会破坏并继续转换到交换机中的下一个案例).

Sal*_*man 12

头函数用于将HTTP响应头发送回用户(即,您不能使用它来创建请求头.

我可以问你为什么要这样做?为什么当你可以在那里模拟POST请求然后对某些数据采取行动?我当然假设script.php驻留在你的服务器上.

要创建POST请求,请使用fsockopen()打开与主机的TCP连接,然后在fsockopen()返回的处理程序上使用fwrite(),其值与您在OP中的头函数中使用的值相同.或者,您可以使用cURL.


Car*_*ade 5

今天非常需要这个答案,因为并不是每个人都希望使用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);

}
Run Code Online (Sandbox Code Playgroud)