7 changed files with 208 additions and 2 deletions
@ -0,0 +1,70 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\admin\service; |
||||
|
|
||||
|
use app\admin\model\Ip as IpModel; |
||||
|
|
||||
|
class IpService |
||||
|
{ |
||||
|
|
||||
|
/** |
||||
|
* 发起请求获取IP |
||||
|
* @return bool |
||||
|
*/ |
||||
|
public static function proxy(): bool |
||||
|
{ |
||||
|
$app_key = "50f95964abbb4b10f9b5e4f1075149d0"; |
||||
|
$url = "https://api.wandoudl.com/api/ip/list-v2?"; |
||||
|
|
||||
|
$params = [ |
||||
|
// 账户密钥 |
||||
|
"app_key" => $app_key, |
||||
|
// 指定套餐,默认不指定 |
||||
|
"pack" => "0", |
||||
|
// 获取代理IP数量 |
||||
|
"num" => "20", |
||||
|
// 代理协议 http协议: 1 |
||||
|
"xy" => "1", |
||||
|
// 格式 1:txt 2-json |
||||
|
"type" => "2", |
||||
|
// 支持自定义间隔符 |
||||
|
"lb" => "\r\n", |
||||
|
// 是否去重,默认不去重,不去重=0,去重=99 |
||||
|
"nr" => "0", |
||||
|
// 指定地区,默认全国,支持指定省或市,支持同时指定多个,英文符号 | 分隔 |
||||
|
// 全国: 0 |
||||
|
// 北京市: 110000 |
||||
|
// 北京市+江苏省+杭州市: 110000|320000|330100 |
||||
|
"area_id" => "0", |
||||
|
// |
||||
|
"isp" => 0, |
||||
|
]; |
||||
|
|
||||
|
$data = self::curl($url, $params); |
||||
|
|
||||
|
return IpModel::updateIp($data); |
||||
|
} |
||||
|
|
||||
|
protected static function curl($url, $params) |
||||
|
{ |
||||
|
$ch = curl_init(); |
||||
|
// 设置请求地址 |
||||
|
curl_setopt($ch, CURLOPT_URL, $url . http_build_query($params)); |
||||
|
// https请求 不验证证书和hosts |
||||
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); |
||||
|
// 响应内容返回而不是直接输出 |
||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||||
|
// 请求超时时间 秒 |
||||
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30); |
||||
|
// 发出网络请求 |
||||
|
$output = curl_exec($ch); |
||||
|
$result = json_decode($output, true); |
||||
|
if (isset($result['code'])) { |
||||
|
if ($result['code'] == 200) { |
||||
|
return $result['data']; |
||||
|
} |
||||
|
} |
||||
|
return []; |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,47 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\common\command; |
||||
|
|
||||
|
use app\admin\model\Ip as IpModel; |
||||
|
use think\console\Command; |
||||
|
use think\console\Input; |
||||
|
use think\console\Output; |
||||
|
|
||||
|
class IpUpdate extends Command |
||||
|
{ |
||||
|
|
||||
|
protected function configure() |
||||
|
{ |
||||
|
$this->setName('ip_up') |
||||
|
->setDescription('系统节点刷新服务'); |
||||
|
} |
||||
|
|
||||
|
protected function execute(Input $input, Output $output) |
||||
|
{ |
||||
|
$output->writeln("========正在更新IP状态:=====" . date('Y-m-d H:i:s')); |
||||
|
$check = $this->update(); |
||||
|
$check !== true && $output->writeln("更新IP状态失败:" . $check); |
||||
|
$output->writeln("更新IP状态完成:" . date('Y-m-d H:i:s')); |
||||
|
} |
||||
|
|
||||
|
protected function update(): bool |
||||
|
{ |
||||
|
$ipModel = new IpModel(); |
||||
|
$time = time(); |
||||
|
|
||||
|
$where = [['status', '=', 1], ['expire_time', '<', $time]]; |
||||
|
$idArr = $ipModel->where($where)->column('id'); |
||||
|
|
||||
|
if ($idArr) { |
||||
|
$res = $ipModel->where('id', 'in', $idArr)->save(['status' => 0]); |
||||
|
if (!$res) { |
||||
|
return '数据更新失败'; |
||||
|
} |
||||
|
} else { |
||||
|
return '占无修改数据'; |
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue