PHP带重试功能的curl

简介: 2016年1月13日 10:48:10 星期三 1 /** 2 * @param string $url 访问链接 3 * @param string $target 需要重试的标准: 返回结果中是否包含$target字符串 4 * @param int $retry ...

2016年1月13日 10:48:10 星期三

 1 /**
 2  * @param string   $url 访问链接
 3  * @param string $target 需要重试的标准: 返回结果中是否包含$target字符串
 4  * @param int $retry 重试次数, 默认3次
 5  * @param int $sleep 重试间隔时间, 默认1s
 6  * @return bool|mixed curl返回结果
 7  * desc 有重试功能的curlget
 8  */
 9 function curlGetRetry($url, $target, $retry=3, $sleep = 1)
10 {
11     $ch = curl_init();
12     curl_setopt($ch, CURLOPT_URL, $url);
13     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
14     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
15     curl_setopt($ch, CURLOPT_TIMEOUT, 5);
16     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
17     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); // 检查证书中是否设置域名(为0也可以,就是连域名存在与否都不验证了)
18 
19     $output = curl_exec($ch);
20 
21     while((strpos($jsonOutput, $target) === FALSE) && $retry--){ //检查$targe是否存在
22         sleep($sleep); //阻塞1s
23         $output = curl_exec($ch);
24     }
25     curl_close($ch);
26     return $output;
27 }

 

目录
相关文章
|
3月前
|
SQL PHP 数据库
|
4月前
|
JSON PHP 数据格式
PHP curl方法封装
PHP curl方法封装
31 0
|
7月前
|
安全 Java PHP
PHP/JAVA交易所系统开发(成熟案例)丨需求步骤丨指南详细丨方案逻辑丨逻辑教程丨源码功能
An exchange refers to an institution or platform that provides a centralized market for buying and selling transactions, where participants can trade various assets, such as securities, commodities, cryptocurrencies, etc. Exchanges provide market infrastructure and rules to facilitate compliant, saf
|
6天前
|
数据采集 监控 API
使用PHP实现动态代理IP的功能
使用PHP实现动态代理IP的功能
|
1月前
|
PHP 数据格式
PHP 中的CURL 模拟表单的post提交
PHP 中的CURL 模拟表单的post提交
16 0
|
6月前
|
PHP 数据库 数据安全/隐私保护
PHP写用户注册、登录和密码重置功能
PHP写用户注册、登录和密码重置功能
php案例:自己写个数组转换成对象 对象转换成数组的的功能出来吧
php案例:自己写个数组转换成对象 对象转换成数组的的功能出来吧
php案例:自己写个数组转换成对象 对象转换成数组的的功能出来吧