url is_readable 읽을 수 있는 파일인가?
내장함수인 is_readable 함수는 파일이 있는지 그리고 읽을 수 있는 권한이 있는지 까지 체크를 해주는 함수인데 URL 체크기능은 없어 만들어진 클래스 입니다.
USER_AGENT는 자주 변경 될 수 있어 외부의 글로벌 변수가 있으면 그것을 사용 합니다.
그리고 ssl 인증서 무시할것인지.
네트워크를 통한 접속의 시간을 제한
페이지가 이동 되었을때 자동으로 찾아서 이동 할것인 선택하는 옵션이 있습니다.
class PabburiUtil
{
static function is_readable( $file, $ssl_validation=false, $timeOut=3, $followLocation=true ) {
global $USER_AGENT;
if ( !isset($USER_AGENT) || strlen($USER_AGENT) < 20 ) {
$USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36';
}
$rReadable = false;
if ( stristr($file, 'http://') || stristr($file, 'https://') )
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $file);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_USERAGENT, $USER_AGENT);
curl_setopt($curl, CURLOPT_TIMEOUT, $timeOut);
if ( $followLocation ) {
curl_setopt ($ch,CURLOPT_FOLLOWLOCATION , $followLocation);
}
// 인증서 유효성 무시
if ( $ssl_validation ) {
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST , false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER , false);
}
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($httpCode == 200) {
$rReadable = true;
// 여기서 필요한 추가 로직을 수행할 수 있습니다.
// 예: 파일 내용 가져오기 등
//$fileContents = file_get_contents($file);
//$parsedData = json_decode($fileContents);
//$rReadable = !empty($parsedData);
// 가져온 헤더 정보 출력하기 (선택사항)
//$headerSize = curl_getinfo($curl,CURLINFO_HEADER_SIZE);
//$headers = substr($response, 0 , $headerSize);
//echo "Headers: ".$headers;
}
curl_close ($curl);
}
else {
$rReadable = is_readable($file);
}
return $rReadable;
}
}
$rr = PabburiUtil::is_readable($file, false, 5, true);
echo '$rr: ' . $rr . "<br>" . PHP_EOL;