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\r\n";
        $header.="Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
        $header.="Accept-language: zh-cn,zh;q=0.5\r\n";
        $header.="Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7\r\n";
        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;
    }

 

posted on 2015-05-11 11:03  lsl8966  阅读(408)  评论(0编辑  收藏  举报