
file_get_contents 함수는 웹페이지의 내용을 가져올 때 캐싱을 자동으로 적용하므로, 캐시를 제거하거나 설정하는 방법은 다음과 같습니다.
1. `file_get_contents` 함수의 `stream_context_create` 옵션을 사용하여 캐시를 제거할 수 있습니다. 캐시를 제거하려면 `stream_context_create` 함수의 `http` 옵션의 `method` 값을 `GET`으로 설정하고, `http` 옵션의 `header` 값을 `Cache-Control: no-cache`로 설정합니다.
#hostingforum.kr
php
$opts = array(
'http' => array(
'method' => 'GET',
'header' => 'Cache-Control: no-cache'
)
);
$context = stream_context_create($opts);
$html = file_get_contents('https://example.com', false, $context);
2. `file_get_contents` 함수의 `stream_context_create` 옵션을 사용하여 캐시를 설정할 수 있습니다. 캐시를 설정하려면 `stream_context_create` 함수의 `http` 옵션의 `method` 값을 `GET`으로 설정하고, `http` 옵션의 `header` 값을 `Cache-Control: max-age=3600`로 설정합니다. 이 설정으로 캐시가 1시간 동안 유지됩니다.
#hostingforum.kr
php
$opts = array(
'http' => array(
'method' => 'GET',
'header' => 'Cache-Control: max-age=3600'
)
);
$context = stream_context_create($opts);
$html = file_get_contents('https://example.com', false, $context);
타임아웃을 설정하는 방법은 `stream_context_create` 함수의 `http` 옵션의 `timeout` 값을 설정하는 것입니다.
#hostingforum.kr
php
$opts = array(
'http' => array(
'method' => 'GET',
'timeout' => 10 // 10초
)
);
$context = stream_context_create($opts);
$html = file_get_contents('https://example.com', false, $context);
이러한 방법으로 캐시를 제거하거나 설정하고 타임아웃을 설정할 수 있습니다.
2025-04-08 18:29