개발! 딱 깔끔하고 센스있게!

Guzle HTTP PHP 클라이언트 연결 제한 시간 사용하기 본문

개발 스크랩 메모/PHP

Guzle HTTP PHP 클라이언트 연결 제한 시간 사용하기

렉사이 2020. 12. 17. 23:55

나는 구즈le으로 url-s 목록을 열어 제목을 얻었다.

몇몇 url 응답 시간이 너무 길어서 열 수 없어, 나는 그것들을 무시하고 싶다.

Guzle이 이상을 던지기 전에 나는 최대 20초 이상 시간을 바꾸고, 연결 시간을 2초로 제한하고 싶다.

나는 이 코드가 있는데, 그것은 여전히 더 오랜 시간이 필요하다.

format("d.m.Y H:i:s"); echo $start."\n"; $client = new Guzzle\Http\Client(); Guzzle\Http\StaticClient::mount(); try { $request = $client->get('http://takestoolongexample', [], ['connect_timeout' => 2, 'timeout' => 3, 'debug' => true]); $response = $request->send(); var_dump($response->getStatusCode()); } catch (Exception $e) { echo "\n".$e->getMessage()."\n"; } $end = new \DateTime("now"); $end = $end->format("d.m.Y H:i:s"); echo "\n".$end."\n"; ?> 

다음은 예례 결과다.

네가 본 것처럼 13초 걸렸다.

$ php test.php 30.12.2013 22:00:07 * getaddrinfo(3) failed for takestoolongexample:80 * Couldn't resolve host 'takestoolongexample' * Closing connection 0 [curl] 6: Couldn't resolve host 'http://takestoolongexample' http://takestoolongexample 30.12.2013 22:00:20 

(http://takestoolongexample 진정한 url, 여기서 변경)

대답

guzle 버전 (Guzzle 4) 의 최신 해결 방안이다.

$request = $client->get(sprintf("%s/noisesize.api", $this->noiseConfig->url), [ 'timeout' => 5, // Response timeout 'connect_timeout' => 5, // Connection timeout ]); 

Guzle Htp Excception Requestexception

최신 버전의 문서는 여기에 있습니다: 구즈클 요청 옵션 – 연결이 초과되었습니다.

Comments