Programing

PHP에서 컬 타임 아웃 설정

lottogame 2020. 5. 5. 19:30
반응형

PHP에서 컬 타임 아웃 설정


PHP를 통해 eXist 데이터베이스에서 curl 요청을 실행하고 있습니다. 데이터 집합이 매우 커서 결과적으로 데이터베이스가 XML 응답을 반환하는 데 오랜 시간이 걸립니다. 이를 해결하기 위해 긴 시간 초과로 컬 요청을 설정했습니다.

$ch = curl_init();
$headers["Content-Length"] = strlen($postString);
$headers["User-Agent"] = "Curl/1.0";

curl_setopt($ch, CURLOPT_URL, $requestUrl);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, 'admin:');
curl_setopt($ch,CURLOPT_TIMEOUT,1000);
$response = curl_exec($ch);
curl_close($ch);

그러나 컬 요청은 요청이 완료되기 전에 지속적으로 종료됩니다 (브라우저를 통해 요청하면 1000 미만). 컬이 타임 아웃을 설정하는 올바른 방법인지 아는 사람이 있습니까?


설명서를 참조하십시오 : http://www.php.net/manual/en/function.curl-setopt.php

CURLOPT_CONNECTTIMEOUT-연결하는 동안 대기하는 시간 (초)입니다. 무한정 대기하려면 0을 사용하십시오.
CURLOPT_TIMEOUT-cURL 함수를 실행할 수있는 최대 시간 (초)입니다.

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); 
curl_setopt($ch, CURLOPT_TIMEOUT, 400); //timeout in seconds

또한 PHP 스크립트 자체의 시간 실행을 늘리는 것을 잊지 마십시오.

set_time_limit(0);// to infinity for example

흠, 그것은 CURLOPT_TIMEOUTcURL 함수가 실행하는 데 걸리는 시간을 정의하는 것처럼 보입니다 . CURLOPT_CONNECTTIMEOUTcURL에 연결이 완료되기를 기다리는 최대 시간을 알려주기 때문에 실제로 대신 보고 있어야한다고 생각합니다 .


코드는 시간 초과를 1000 초로 설정합니다 . 밀리 초의 경우을 사용하십시오 CURLOPT_TIMEOUT_MS.


PHP docs 주석에서 일부 사람들과 관련이있을 수있는 단점이 있습니다.

cURL이 1 초 이내에 시간 초과되도록하려면 CURLOPT_TIMEOUT_MS"Unix-like systems"에 버그 / "기능"이 있지만 값이 <1000ms 인 경우 오류가 발생하면 libcurl이 즉시 시간 초과되도록하는 버그 / 기능이 있습니다. 오류 (28) : 시간이 초과되었습니다 " 이 동작에 대한 설명은 다음과 같습니다.

"libcurl이 표준 시스템 이름 확인자를 사용하도록 빌드 된 경우, 전송의 해당 부분은 최소 1 초의 시간 제한이있는 시간 초과에 대해 여전히 전체 초 해상도를 사용합니다."

이것이 PHP 개발자에게 의미하는 바는 "libcurl이 표준 시스템 이름 확인자를 사용하고 있는지 알 수 없기 때문에 먼저 테스트하지 않고는이 기능을 사용할 수 없습니다."

문제는 (Li | U) nix에서 libcurl이 표준 이름 확인자를 사용할 때 libcurl이 시간 초과 경보라고 생각하는 이름 확인 중에 SIGALRM이 발생한다는 것입니다.

해결책은 CURLOPT_NOSIGNAL을 사용하여 신호를 비활성화하는 것입니다. 다음은 시간 초과를 테스트 할 수 있도록 10 초 지연을 발생시키는 자체 요청 스크립트 예입니다.

if (!isset($_GET['foo'])) {
    // Client
    $ch = curl_init('http://localhost/test/test_timeout.php?foo=bar');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200);
    $data = curl_exec($ch);
    $curl_errno = curl_errno($ch);
    $curl_error = curl_error($ch);
    curl_close($ch);

    if ($curl_errno > 0) {
        echo "cURL Error ($curl_errno): $curl_error\n";
    } else {
        echo "Data received: $data\n";
    }
} else {
    // Server
    sleep(10);
    echo "Done.";
}

에서 http://www.php.net/manual/en/function.curl-setopt.php#104597


You will need to make sure about timeouts between you and the file. In this case PHP and Curl.

To tell Curl to never timeout when a transfer is still active, you need to set CURLOPT_TIMEOUT to 0, instead of 1000.

curl_setopt($ch, CURLOPT_TIMEOUT, 0);

In PHP, again, you must remove time limits or PHP it self (after 30 seconds by default) will kill the script along Curl's request. This alone should fix your issue.
In addition, if you require data integrity, you could add a layer of security by using ignore_user_abort:

# The maximum execution time, in seconds. If set to zero, no time limit is imposed.
set_time_limit(0);

# Make sure to keep alive the script when a client disconnect.
ignore_user_abort(true);

A client disconnection will interrupt the execution of the script and possibly damaging data,
eg. non-transitional database query, building a config file, ecc., while in your case it would download a partial file... and you might, or not, care about this.

Answering this old question because this thread is at the top on engine searches for CURL_TIMEOUT.


You can't run the request from a browser, it will timeout waiting for the server running the CURL request to respond. The browser is probably timing out in 1-2 minutes, the default network timeout.

You need to run it from the command line/terminal.


If you are using PHP as a fastCGI application then make sure you check the fastCGI timeout settings. See: PHP curl put 500 error

참고URL : https://stackoverflow.com/questions/2582057/setting-curls-timeout-in-php

반응형