>백엔드 개발 >PHP 튜토리얼 >模拟表单提交的例子_PHP

模拟表单提交的例子_PHP

WBOY
WBOY원래의
2016-06-01 12:36:14836검색

// 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));


?>

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.