Skip to content

Commit

Permalink
优化:下载附带 User-Agent 信息、连接超时、重试次数
Browse files Browse the repository at this point in the history
  • Loading branch information
taksssss committed Sep 6, 2024
1 parent e587901 commit 4cc8da0
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions epg/public.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,24 @@ function t2s($channel) {
}

// 下载文件
function downloadData($url, $timeout = 30) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$data = curl_exec($ch);
if (curl_errno($ch)) {
curl_close($ch);
return false;
function downloadData($url, $timeout = 30, $connectTimeout = 10, $retry = 3) {
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_CONNECTTIMEOUT => $connectTimeout,
CURLOPT_HTTPHEADER => [
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
'Accept: */*',
'Connection: keep-alive'
]
]);
while ($retry--) {
$data = curl_exec($ch);
if (!curl_errno($ch)) break;
}
curl_close($ch);
return $data;
return $data ?: false;
}
?>

0 comments on commit 4cc8da0

Please sign in to comment.