PHP请求远程地址设置超时时间
来源:互联网 发布:mac dps文件怎么打开 编辑:程序博客网 时间:2024/05/16 14:19
PHP请求远程地址设置超时时间,主要讲解file_get_contents、fopen、curl这三个简单常用函数设置超时时间的方法,一般情况下建议使用curl,性能最好,效率也最高,。
1、file_get_contents 请求超时设置
1
2
3
4
5
6
7
2
3
4
5
6
7
$timeout = array(
'http'=> array(
'timeout'=>5//设置一个超时时间,单位为秒
)
);
$ctx = stream_context_create($timeout);
$text = file_get_contents("http://www.leixuesong.cn/",0, $ctx);
'http'=> array(
'timeout'=>5//设置一个超时时间,单位为秒
)
);
$ctx = stream_context_create($timeout);
$text = file_get_contents("http://www.leixuesong.cn/",0, $ctx);
2、fopen 请求超时设置
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
$timeout = array(
'http' => array(
'timeout' => 5 //设置一个超时时间,单位为秒
)
);
$ctx = stream_context_create($timeout);
if ($fp = fopen("http://www.leixuesong.cn/", "r", false, $ctx)) {
while( $c = fread($fp, 8192)) {
echo $c;
}
fclose($fp);
}
'http' => array(
'timeout' => 5 //设置一个超时时间,单位为秒
)
);
$ctx = stream_context_create($timeout);
if ($fp = fopen("http://www.leixuesong.cn/", "r", false, $ctx)) {
while( $c = fread($fp, 8192)) {
echo $c;
}
fclose($fp);
}
3、curl请求超时设置
CURL 是常用的访问HTTP协议接口的lib库,性能高,还有一些并发支持的功能等。
curl_setopt($ch, opt) 可以设置一些超时的设置,主要包括:a 、CURLOPT_TIMEOUT 设置cURL允许执行的最长秒数。b、CURLOPT_TIMEOUT_MS 设置cURL允许执行的最长毫秒数。c、 CURLOPT_CONNECTTIMEOUT 在发起连接前等待的时间,如果设置为0,则无限等待。d、 CURLOPT_CONNECTTIMEOUT_MS 尝试连接等待的时间,以毫秒为单位。如果设置为0,则无限等待。e、 CURLOPT_DNS_CACHE_TIMEOUT 设置在内存中保存DNS信息的时间,默认为120秒。
1
2
3
4
5
6
2
3
4
5
6
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,60); //只需要设置一个秒的数量就可以
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,60); //只需要设置一个秒的数量就可以
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);
0 0
- PHP请求远程地址设置超时时间
- PHP请求远程地址设置超时时间
- PHP请求远程地址设置超时时间
- PHP请求远程地址设置超时时间
- 设置 TOMCAT 请求超时时间
- 设置请求超时的时间
- 设置请求超时的时间
- AFNetWorking设置请求超时时间
- http 请求 超时时间设置
- php请求webservice超时设置
- php脚本超时时间设置
- php脚本超时时间设置
- php脚本超时时间设置
- PHP curl 超时时间设置
- php设置socket超时时间
- 设置Apache的HttpClient请求时间超时
- 设置AFNetworking网络请求的超时时间
- 设置 AFNetworking 网络请求的超时时间
- Android ListView优化思路12则
- 根据字符串中出现的字符逐个打印各字符出现次数
- UTF—8与UTF—8(无bom)格式相比有什么不同
- 洛谷 P1781 宇宙总统
- selenium使用Xpath定位的四种方法
- PHP请求远程地址设置超时时间
- [iOS]UIButton同时添加image和title后子视图位置管理
- 听说2017你想写前端?
- nginx 配置多个tomcat共用80端口
- k8s容器调度策略
- 堆排序
- 探究为何:在onCreate中通过View.post能获取宽高
- android网络判断
- git ssh传输的原理与实践