<?php
// 简单的发送方法
// 为了发送到微软的 webservers 平台,你可能需要变换换行 "\n" 到 "\r\n"    // 发送 "browser" 头
    function send_headers ($fp) {
        fputs ($fp, "Accept: */*\n");
        fputs ($fp, "Accept-Language: en\n");
        fputs ($fp, "Connection: Keep-Alive\n");
        fputs ($fp, "User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)\n");
    }    // 发送数据和接受回复
    function post_data ($host, $url, $data) {
        $fp = @fsockopen ($host, 80, $errno, $errstr, 120);
        $ret = "";
                if (strncasecmp ($url, "http://", 7) == 0) $url = substr ($url, 7);
        $req = strchr($url,"/");
        if (! $req) {
            $req = "/";
        }
        if ($fp) {
            fputs ($fp, "POST $req HTTP/1.0\n");
            send_headers ($fp);
            fputs ($fp, "Content-type: application/x-www-form-urlencoded\n");
            $out = "";
            while (list ($k, $v) = each ($data)) {
                if(strlen($out) != 0) $out .= "&";
                $out .= rawurlencode($k). "=" .rawurlencode($v);
            }
            $out = trim ($out);
            fputs ($fp, "Content-length: ".strlen($out)."\n\n"); 
            fputs ($fp, "$out");
            fputs ($fp, "\n");
            while(!feof($fp)){ 
                $ret .= fgets($fp,128); 
            } 
        fclose ($fp);
        }
        return $ret;
    }/* 例子:
发送数据到10.34.137.9/users/ide/server.php
server.php的内容为
<?php
print_r($_POST);
?>
*/
$reply = post_data ("10.34.137.9", "/users/ide/server.php", array ("login" => "用户", "pass" => "口令"));
echo $reply; // 显示返回的结果
?> php能正确接受,cgi应该也可以的。

解决方案 »

  1.   

    一个php文件向另一个地址post数据,不用表单和隐藏的变量
    function posttohost($url, $data) {
    $url = parse_url($url);
    if (!$url) return "couldn't parse url";
    if (!isset($url['port'])) { $url['port'] = ""; }
    if (!isset($url['query'])) { $url['query'] = ""; }$encoded = "";while (list($k,$v) = each($data)) {
    $encoded .= ($encoded ? "&" : "");
    $encoded .= rawurlencode($k)."=".rawurlencode($v);
    }$fp = fsockopen($url['host'], $url['port'] ? $url['port'] : 80);
    if (!$fp) return "Failed to open socket to $url[host]";fputs($fp, sprintf("POST %s%s%s HTTP/1.0\n", $url['path'], $url['query'] ? "?" : "", $url['query']));
    fputs($fp, "Host: $url[host]\n");
    fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
    fputs($fp, "Content-length: " . strlen($encoded) . "\n");
    fputs($fp, "Connection: close\n\n");fputs($fp, "$encoded\n");$line = fgets($fp,1024);
    if (!eregi("^HTTP/1\.. 200", $line)) return;$results = ""; $inheader = 1;
    while(!feof($fp)) {
    $line = fgets($fp,1024);
    if ($inheader && ($line == "\n" || $line == "\r\n")) {
    $inheader = 0;
    }
    elseif (!$inheader) {
    $results .= $line;
    }
    }
    fclose($fp);return $results;
    }
    ?>   也可以这样 
    $URL="www.mysite.com/test.php"; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,"https://$URL"); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, "Data1=blah&Data2=blah");
    curl_exec ($ch); 
    curl_close ($ch); 
    ?>