개발 중에는 여러 HTTP 요청을 동시에 시작 해야하는 상황이 발생할 수 있습니다. PHP의 CURL 라이브러리는 CURL_MULTI_* 함수 인 여러 HTTP 요청을 병렬로 시작하는 매우 편리한 방법을 제공합니다. curl_multi_add_handle 및 curl_multi_close 는이 유형의 작업에서 두 가지 주요 기능입니다. 이 기사에서는 여러 CURL 요청 관리에 올바르게 사용하는 방법을 설명합니다.
Curl은 서버에 요청을 보내고 응답을받는 도구입니다. HTTP, HTTPS, FTP 등과 같은 여러 프로토콜을 지원합니다. PHP에서 CURL은 일련의 함수를 통해 작동 할 수 있으며, 가장 일반적으로 사용되는 것은 Curl_init , CURL_SETOPT 및 CURL_EXEC를 포함합니다.
curl_multi_add_handle 함수는 여러 컬 세션 핸들을 여러 컬 프로세싱 풀에 추가하는 데 사용됩니다. 이를 통해 여러 요청을 병렬로 처리 할 수 있으므로 특히 여러 HTTP 요청을 전송 해야하는 경우 효율성이 향상됩니다.
// 다중 초기화 cURL 세션
$ch1 = curl_init('https://gitbox.net/api/resource1');
$ch2 = curl_init('https://gitbox.net/api/resource2');
// 각각을 설정하십시오 cURL 요청 된 옵션
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
// 얼마나 많은 초기화 cURL 핸들
$multiHandle = curl_multi_init();
// 할 것이다 cURL 핸들추가到多重 cURL 핸들中
curl_multi_add_handle($multiHandle, $ch1);
curl_multi_add_handle($multiHandle, $ch2);
// 요청을 실행하십시오
do {
$status = curl_multi_exec($multiHandle, $active);
if ($active) {
// 활동 요청이 완료되기를 기다립니다
curl_multi_select($multiHandle);
}
} while ($active && $status == CURLM_OK);
// 응답 데이터를 얻습니다
$response1 = curl_multi_getcontent($ch1);
$response2 = curl_multi_getcontent($ch2);
// 출력 결과
echo "Response from resource1: $response1\n";
echo "Response from resource2: $response2\n";
// 각각을 닫습니다 cURL 세션
curl_multi_remove_handle($multiHandle, $ch1);
curl_multi_remove_handle($multiHandle, $ch2);
위의 코드에서 Curl_multi_add_handle은 여러 개의 컬 핸들 $ ch1 과 $ ch2를 여러 컬 핸들 $ multihandle 에 추가 한 다음 Curl_multi_exec을 통해 이러한 요청을 수행합니다. curl_multi_select는 요청이 완료 될 때까지 기다려 병렬 처리를 구현합니다.
모든 요청이 완료되면 여러 컬 핸들을 닫아야합니다. 현재 Curl_multi_Close 함수를 사용하십시오. 이 기능은 Curl_Multi_Init 에 의해 생성 된 핸들과 관련된 리소스를 출시합니다.
// 요청을 완료 한 후 얼마나 닫힐 정도 cURL 핸들
curl_multi_close($multiHandle);
curl_multi_close를 사용한 후 여러 컬 핸들과 관련된 모든 자원이 청소됩니다. 여러 컬 핸들을 닫기 전에 추가 된 모든 컬 핸들을 먼저 제거해야합니다. 그렇지 않으면 리소스 누출이 발생합니다.
다음은 curl_multi_add_handle 및 curl_multi_close를 사용하여 여러 HTTP 요청을 관리하는 방법을 보여주는 완전한 예입니다.
// 다중 초기화 cURL 세션
$ch1 = curl_init('https://gitbox.net/api/resource1');
$ch2 = curl_init('https://gitbox.net/api/resource2');
// 설정 cURL 옵션
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
// 얼마나 많은 초기화 cURL 핸들
$multiHandle = curl_multi_init();
// 추가 cURL 세션到多重处理池
curl_multi_add_handle($multiHandle, $ch1);
curl_multi_add_handle($multiHandle, $ch2);
// 여러 요청을 수행하십시오
do {
$status = curl_multi_exec($multiHandle, $active);
if ($active) {
curl_multi_select($multiHandle); // 요청이 완료 될 때까지 기다리십시오
}
} while ($active && $status == CURLM_OK);
// 반환 된 콘텐츠를 얻으십시오
$response1 = curl_multi_getcontent($ch1);
$response2 = curl_multi_getcontent($ch2);
// 출력 요청 결과
echo "Response from resource1: $response1\n";
echo "Response from resource2: $response2\n";
// 제거하다 cURL 세션
curl_multi_remove_handle($multiHandle, $ch1);
curl_multi_remove_handle($multiHandle, $ch2);
// 얼마나 꺼져 있습니다 cURL 핸들
curl_multi_close($multiHandle);
이 코드는 요청이 완료된 후 여러 HTTP 요청을 병렬로 시작하고 여러 개의 컬 핸들을 닫는 방법을 완전히 보여줍니다.