//默认模拟的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;
}

file_get_contents post数据的更多相关文章

  1. PHP中fopen,file_get_contents,curl函数的区别

    PHP中fopen,file_get_contents,curl函数的区别 1.fopen/file_get_contents每次请求都做DNS查询,并不对DNS的信息进行缓存,而curl会对DNS的 ...

  2. 给file_get_contents函数设置超时时间

    $opts = array( 'http'=>array( 'method'=>"GET", 'timeout'=>60, ) ); $context = str ...

  3. php远程连接http方式

    以下这三者是通过GET方式来获取数据 1.socket 方式 一般是指定网址.端口号.超时时间. 再对报头进行修改以及传递参数,包括:header.method.content, 返回的内容包括hea ...

  4. file_get_contents模仿浏览器头(user_agent)获取数据

    本篇文章是对file_get_contents模仿浏览器头(user_agent)获取数据进行了详细的分析介绍,需要的朋友参考下     什么是user agentUser Agent中文名为用户代理 ...

  5. PHP file_get_contents函数读取远程数据超时的解决方法

    PHP file_get_contents函数读取远程数据超时的解决方法 投稿:junjie 字体:[增加 减小] 类型:转载   这篇文章主要介绍了PHP file_get_contents函数读取 ...

  6. file_get_contents无法获取数据的一种情况

    下面这段php代码突然不好使了,返回的 $html 为空,百思不得解.网上说法好多,但都是一家之言,解决不了我的问题.(我的解决方法也是一家之言,只能解决file_get_contents获取不到数据 ...

  7. php中使用Curl、socket、file_get_contents三种方法POST提交数据

    抓取远程内容,之前一直都在用file_get_content函数,其实早就知道有curl这么一个好东西的存在,但是看了一眼后感觉使用颇有些复杂,没有file_get_content那么简单,再就是需求 ...

  8. file_get_contents函数获取不到数据的一种情况

    问题:  file_get_contents($url) 获取不到数据,尽管URL地址正确,函数使用正确.如下代码 $url = "https://www.baidu.com"; ...

  9. file_get_contents('php://input') 数据如何转换成数组

    前台表单页:demo01.html 后台:demo01.php 输出结果: 备注:若前台通过Ajax的post提交过来的是json数据,需要对json数据进行解析:$data = json_decod ...

随机推荐

  1. mac使用小技

    xcodeブラックスクリーンの解決策: 1.cd ~/Library/Developer/Xcode/DerivedData 2.rm -fr *    //注释:-fr和*是分开的3.关闭模拟器,关 ...

  2. Google Chrome浏览器的使用方法

    Google Chrome浏览器 [原文地址:http://www.cnblogs.com/QLeelulu/archive/2011/08/28/2156402.html ] 在Google Chr ...

  3. linux使用工具记录

    linux工具查询手册: http://linuxtools-rst.readthedocs.io/zh_CN/latest/index.html

  4. web中webAppRootKey作用

    <context-param> <param-name>webAppRootKey</param-name> <param-value>bgn.root ...

  5. asp.net mvc 注册中的邮箱激活功能实现(二)

    邮件发送功能封装 /// <summary>        /// 发送注册邮件        /// </summary>        /// <param name ...

  6. SQL Server 恢复过程

    在恢复过程中.只会分析那些自最后一个检查点之后发生的更改,以确定是否需要重做还是撤销. 在最后一个检查点之前完成的操作都会精确的反应到数据文件中,恢复过程不需要做其它的事. 第一阶段: 分析. 这个阶 ...

  7. COB (Chip On Board) 製程介紹/簡介/注意事項 II

    銀膠 (Silver glue) 如果晶圓有接地或是散熱需求時,一般都會採用[銀膠],如果沒有的話則會採用[厭氧膠].[厭氧膠]顧名思義就是阻隔它與空氣接觸後就會自然固化,不需要高溫烘烤.使用銀膠則需 ...

  8. 7816的报文结构——APDU

    命令APDU 包括头和主体(这可以在上面的图中看到).头包括CLA,INS,P1 和P2 域.同T0 协议一样,CLA 和INS 说明了应用的分类和指令.P1 和P2 用来详细说明具体指令,并由每一条 ...

  9. urllib2.URLError: <urlopen error [Errno 104] Connection reset by peer>

    http://www.dianping.com/shop/8010173 File "综合商场1.py", line 152, in <module>    httpC ...

  10. 在iOS当中发送电子邮件和短信

    iOS实现发送电子邮件的方法很简单,首先导入MessageUI.framework框架,然后代码如下: #import "RPViewController.h" //添加邮件头文件 ...