file_get_contents post数据

//默认模拟的header头
    private function _defaultHeader() {
        $header = "User-Agent:Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12
";
        $header.="Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
";
        $header.="Accept-language: zh-cn,zh;q=0.5
";
        $header.="Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7
";
        return $header;
    }
private function post($url, $post_data=array(), $timeout=5, $header="") {
        $header = empty($header) ? self::_default_header() : $header;
        $post_string = http_build_query($post_data);
        $header.="Content-length: " . strlen($post_string);
        $opts = array(
            'http' => array(
                'protocol_version' => '1.0', //http协议版本(若不指定php5.2系默认为http1.0)
                'method' => "POST", //获取方式
                'timeout' => $timeout, //超时时间
                'header' => $header,
                'content' => $post_string)
        );
        $context = stream_context_create($opts);
        //为解决接口超时
        $cnt=0;
        while($cnt < 3 && ($str=@file_get_contents($url, false, $context))===FALSE)
            $cnt++;
        return $str;
    }
现主要从事PHP、Uinx/Linux、C/C++等方面的项目开发。
原文地址:https://www.cnblogs.com/lsl8966/p/4493962.html