file_get_contents()
루프 에서 메소드를 사용하여 일련의 링크를 호출하고 있습니다. 각 링크를 처리하는 데 15 분 이상 걸릴 수 있습니다. 이제 PHP file_get_contents()
에 시간 초과 기간이 있는지에 대해 걱정하고 있습니까?
그렇다면 통화 시간이 초과되고 다음 링크로 이동합니다. 이전 링크를 끝내지 않고 다음 링크를 호출하고 싶지 않습니다.
file_get_contents()
타임 아웃 기간이 있는지 알려주세요 . 를 포함하는 파일 file_get_contents()
은 set_time_limit()
0 (무제한)으로 설정됩니다 .
답변
기본 시간 초과는 default_socket_timeout
ini-setting에 의해 정의되며 60 초입니다. 즉시 변경할 수도 있습니다.
ini_set('default_socket_timeout', 900); // 900 Seconds = 15 Minutes
시간 초과를 설정하는 다른 방법 은 사용중인 HTTP 스트림 래퍼 의 HTTP 컨텍스트 옵션 으로 stream_context_create
시간 초과를 설정하는 데 사용 하는 것입니다.
$ctx = stream_context_create(array('http'=>
array(
'timeout' => 1200, //1200 Seconds is 20 Minutes
)
));
echo file_get_contents('http://example.com/', false, $ctx);
답변
@diyism에서 언급했듯이 ” default_socket_timeout, stream_set_timeout 및 stream_context_create timeout은 전체 연결 시간 초과가 아니라 모든 행 읽기 / 쓰기의 시간 초과입니다. “@stewe의 최고 답변이 실패했습니다.
을 사용하는 대신 file_get_contents
항상 curl
시간 초과와 함께 사용할 수 있습니다 .
링크 호출에 작동하는 작동 코드가 있습니다.
$url='http://example.com/';
$ch=curl_init();
$timeout=5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$result=curl_exec($ch);
curl_close($ch);
echo $result;
답변
예! 세 번째 매개 변수에 스트림 컨텍스트 를 전달하면 다음과 같습니다.
시간 제한이 1 초입니다 .
file_get_contents("https://abcedef.com", 0, stream_context_create(["http"=>["timeout"=>1]]));
https://www.php.net/manual/en/function.file-get-contents.php의 주석 섹션에있는 소스
method
header
user_agent
content
request_fulluri
follow_location
max_redirects
protocol_version
timeout
다른 상황 : https://www.php.net/manual/en/context.php
답변
default_socket_timeout 을 즉시 변경하면 file_get_contents 호출 후 해당 값을 복원하는 것이 유용 할 수 있습니다 .
$default_socket_timeout = ini_get('default_socket_timeout');
....
ini_set('default_socket_timeout', 10);
file_get_contents($url);
...
ini_set('default_socket_timeout', $default_socket_timeout);
답변
내 호스트에서 php.ini를 변경하면 작동합니다.
; Default timeout for socket based streams (seconds)
default_socket_timeout = 300