12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- /**
- * Created by PhpStorm.
- * User: hy
- * Date: 2019-05-16
- * Time: 13:41
- */
- class IpAgencyService
- {
- /**
- * 获取ip代理
- * @return mixed
- */
- public static function getIpAgency() {
- // 再redis中存10秒,在去请求新的代理
- $res = RedisModel::get('ipproxyPort');
- if($res){
- return json_decode($res);
- }
-
- $url = 'http://mvip.piping.mogumiao.com/proxy/api/get_ip_bs?appKey=f40abf802fec41f3ab723e0bcf529ab0&count=5&expiryDate=0&format=1&newLine=2';
- $result = self::curl('GET',$url);
- if (isset($result->code) && $result->code == 0) {
-
- RedisModel::set('ipproxyPort', json_encode($result->msg));
- RedisModel::expire('ipproxyPort', 10);
- // 之前的代码看的不是特别明白,限制出现了限制直接把返回的代理缓存30秒,如果超过了30秒再去访问
-
-
- return $result->msg;
-
-
- }else {
- if (isset($result->code) && $result->code == 3001) { //接口频率限制, 每次10s
- sleep(1);
- return self::getIpAgency();
- }
- die();
- }
- }
- private static function curl($method, $url, $data=array(), $headers=array()) {
- $ch = curl_init();//初始化curl
- curl_setopt($ch, CURLOPT_URL,$url);
- curl_setopt($ch, CURLOPT_HEADER, 0);//
- // curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_TIMEOUT, 10); //单位 秒,也可以使用
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- if($method == 'post'){
- curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
- }
- $data = curl_exec($ch);//运行curl
- curl_close($ch);
- return json_decode($data);
- }
- }
|