给file_get_contents函数设置超时时间

$opts = array(
‘http‘=>array(
‘method‘=>"GET",
‘timeout‘=>60,
)
);

$context = stream_context_create($opts);

$html =file_get_contents(‘http://www.example.com‘, false, $context);

这样,file_get_contents获取数据时,超出60秒将会自动退出.

还可以利用file_get_contents函数实现post功能

function Post($url, $post = null)
{
$context = array();

if (is_array($post)) {
ksort($post);

$context[‘http‘] = array (
‘timeout‘=>60,
‘method‘ => ‘POST‘,
‘content‘ => http_build_query($post, ‘‘, ‘&‘),
);
}

return file_get_contents($url, false, stream_context_create($context));
}

$data = array (
‘name‘ => ‘test‘,
‘email‘ => ‘[email protected]‘,
‘submit‘ => ‘submit‘,
);

echo Post(‘http://www.example.com‘, $data);

给file_get_contents函数设置超时时间

时间: 2024-08-09 06:35:23

给file_get_contents函数设置超时时间的相关文章

socket为send和recv设置超时时间

linux和windows下用setsockopt设置SO_SNDTIMEO,SO_RCVTIMEO的参数的一点区别 UDP的socket在某些情况:如对方关闭时,本地可能sendto不出去数据,然后recvfrom就会被阻塞,这时就需要设置 这两个参数的值提高程序质量. linux: struct timeval timeout={3,0};//3s    int ret=setsockopt(sock_fd,SOL_SOCKET,SO_SNDTIMEO,(const char*)&timeo

java测试网络连接是否成功并设置超时时间

/** * 获取RMI接口状态 * * @return "0":服务正常,"1": 连接报错,"2":连接超时 */ @Override public String getRMIJkzt() { final ExecutorService es = Executors.newFixedThreadPool(1); Callable<String> callable = new Callable<String>() {//使

设置超时时间(项目案例仅供参考)

#设置超时时间爬取网页速度相对要快些#encoding:utf8from lxml import etree#xpathimport re#正则import time#时间import requests#传值from selenium import webdriver#通用阅览器from selenium.webdriver.support.ui import WebDriverWait#引用超时模块import random#自定义模块url = '(网址)'driver = webdrive

C# UdpClient 设置超时时间

/********************************************************************** * C# UdpClient 设置超时时间 * 说明: * 网络通信中设置超时时间是常有的时,记录UDP获取.发送超时设置方法. * * 2016-12-8 深圳 南山平山村 曾剑锋 *********************************************************************/ 一.参考文档: 1. Can

设置超时时间

// 设置超时时间 [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"]; manager.requestSerializer.timeoutInterval = 10.f; [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"]; http://www.itjhwd.com/ios-xuexzlzl/

apache httpclient4 设置超时时间

旧的方法(已被禁用) CloseableHttpClient httpclient = HttpClients.createDefault(); httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,5000); httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,5000); 新的方法 HttpPost httpp

如何为session设置超时时间?

要为session设置超时时间,首先要清楚怎么样算是超时, 超时时间是session自上一次活跃时间到本次开始活跃时间之间的间隔时间大于设置的session超时时间. 为session设置超时时间常用的方法有三种: 第一种方法: HttpSession session = request.getSession(true); session.setAttribute("user", user); session.setMaxInactiveInterval(30); 注意:单位是秒,设置

Mybatis设置超时时间

Mybatis设置超时时间 mybatis如果不指定,默认超时时间是不做限制的,默认值为0.mybatis sql配置超时时间有两种方法: 1.全局配置 在mybatis配置文件的settings节点中,增加如下配置<settings>  <setting name="defaultStatementTimeout" value="25"/>  </settings> 以秒为单位的全局sql超时时间设置,当超出了设置的超时时间时,

python调用函数设置超时机制

有时候需要给函数设置超时机制,以防止它卡住我们的程序,这里可以用python的signal模块,signal模块可以实现程序内部的信号处理. # coding:utf8 import time import signal # 自定义超时异常 class TimeoutError(Exception): def __init__(self, msg): super(TimeoutError, self).__init__() self.msg = msg def time_out(interval