首页 > php教程 > php手册 > 正文

simple post method

WBOY
发布: 2016-06-21 09:07:49
原创
1175 人浏览过

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



相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板