欢迎光临
我们一直在努力

模拟表单提交的例子-PHP教程,PHP应用

建站超值云服务器,限时71元/月

<?php
// simple post method
// version 1.0 by andrus (andrus@vnet.ee)

// disclaimer:
// everyone can change or use this code however and wherever they want 😉

// nb! for posting to microsoft platform webservers you may need to change newlines "\n" to "\r\n"
// microsoft always breaks standards …

    // send out "browser" headers
    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");
    }

    // post data and return reply
    function post_data ($host, $url, $data) {
        $fp = @fsockopen ($host, 80, $errno, $errstr, 120);
        $ret = "";
                if (strncasecmp ($url, "http://", 7) == 0) $url = substr ($url, 7);
        $p = strpos ($url, /);
        if (empty ($p)) {
            $req = "/";
        } else {
            $req = substr ($url, $p);
        }
        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;
    }
// example how to use:

// following code will post variables "login" and "pass" to server "www.something.com" script "/submit.php"
$reply = post_data ("www.something.com", "/submit.php", array ("login" => $username, "pass" => $password));

?>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 模拟表单提交的例子-PHP教程,PHP应用
分享到: 更多 (0)