diff --git a/app/common/components/Curl.php b/app/common/components/Curl.php index 406f183..11fd121 100644 --- a/app/common/components/Curl.php +++ b/app/common/components/Curl.php @@ -93,15 +93,16 @@ class Curl * 简易POST * @param string $url * @param array|object|string $data + * @param bool $asJson * @return false|string */ - public static function http_post(string $url, $data) + public static function http_post(string $url, $data, bool $asJson = false) { $opts = array( 'http' => array( 'method' => 'POST', - 'header' => 'Content-type: ' . static::CONTENT_TYPE_DEFAULT, - 'content' => (is_array($data) || is_object($data)) ? http_build_query($data) : $data, + 'header' => 'Content-type: ' . ($asJson ? static::CONTENT_TYPE_JSON : static::CONTENT_TYPE_DEFAULT), + 'content' => (is_array($data) || is_object($data)) ? ($asJson ? json_encode($data, JSON_UNESCAPED_UNICODE) : http_build_query($data)) : $data, 'timeout' => 5 ), // 解决SSL证书验证失败的问题 diff --git a/app/functions.php b/app/functions.php index 5c6b478..e6c7017 100644 --- a/app/functions.php +++ b/app/functions.php @@ -135,33 +135,3 @@ function dataSize($bytes, string $delimiter = '', int $decimals = 2):string return number_format($bytes, $decimals) . $delimiter . $type[$i]; } - -/** - * 大写 PP 调试函数(CLI使用) - * @param mixed $data - * @param bool $echo - * @return string - */ -function PP($data, bool $echo = false) -{ - $hr = PHP_EOL.'******************************'.\date('Y-m-d H:i:s').PHP_EOL; - $str = $hr; - switch (true) { - case is_bool($data): - $show_data = $data ? 'true' : 'false'; - break; - case is_null($data): - $show_data = 'null'; - break; - default: - $show_data = print_r($data, true); - break; - } - $str .= $show_data; - $str .= $hr; - if ($echo) { - echo $str; - return ''; - } - return $str; -} diff --git a/bin/iyuu.php b/bin/iyuu.php index 8b300c7..d960f29 100644 --- a/bin/iyuu.php +++ b/bin/iyuu.php @@ -1,5 +1,6 @@ $text, 'desp' => $desp - )); - $opts = array('http' => array( - 'method' => 'POST', - 'header' => 'Content-type: application/x-www-form-urlencoded', - 'content' => $postdata - )); - $context = stream_context_create($opts); - $result = file_get_contents('https://iyuu.cn/'.$token.'.send', false, $context); - return $result; + ); + return ICurl::http_post('https://iyuu.cn/'.$token.'.send', $data); } } diff --git a/src/Rss/AbstractRss.php b/src/Rss/AbstractRss.php index ed03c00..743aa53 100644 --- a/src/Rss/AbstractRss.php +++ b/src/Rss/AbstractRss.php @@ -221,7 +221,7 @@ abstract class AbstractRss $torrent['filename'] = $id.'.torrent'; $torrent['type'] = 0; // 免费0 $torrent['time'] = date("Y-m-d H:i:s", $time); - $torrent['size'] = getFilesize($length); + $torrent['size'] = dataSize($length); $torrent['length'] = $length; $torrent['guid'] = $guid; $items[] = $torrent; diff --git a/src/helper.php b/src/helper.php index a014fe9..1d4ade0 100644 --- a/src/helper.php +++ b/src/helper.php @@ -69,29 +69,30 @@ function download($url, $cookies='', $useragent='', $method = 'GET') "Content-Type:application/x-www-form-urlencoded", 'User-Agent: '.$useragent); $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); if ($method === 'POST') { curl_setopt($ch, CURLOPT_POST, true); } if (stripos($url, 'https://') === 0) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); - curl_setopt($ch, CURLOPT_SSLVERSION, 1); + //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + //curl_setopt($ch, CURLOPT_SSLVERSION, 1); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); - curl_setopt($ch, CURLOPT_COOKIE, $cookies); - curl_setopt($ch, CURLOPT_URL, $url); + if (!empty($cookies)) { + curl_setopt($ch, CURLOPT_COOKIE, $cookies); + } curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt($ch, CURLOPT_TIMEOUT, 600); + // 2021年7月2日02:04:22 + #curl_setopt($ch, CURLOPT_HEADER, false); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 自动跳转,跟随请求Location + curl_setopt($ch, CURLOPT_MAXREDIRS, 2); // 递归次数 + $data = curl_exec($ch); $status = curl_getinfo($ch); curl_close($ch); - if (isset($status['http_code']) && $status['http_code'] == 200) { - return $data; - } - if (isset($status['http_code']) && $status['http_code'] == 302) { - return download($status['redirect_url'], $cookies, $useragent); - } return $data; } @@ -120,38 +121,6 @@ function convertToMB($from) } } -/** - * 字节数Byte转换为KB、MB、GB、TB - * @param $num - * @return string - */ -function getFilesize($num) -{ - $p = 0; - $format='bytes'; - if ($num>0 && $num<1024) { - return number_format($num).' '.$format; - } - if ($num>=1024 && $num=pow(1024, 2) && $num=pow(1024, 3) && $num=pow(1024, 4) && $num