PHPcURLタイムアウト設定CURLOPT_CONNECTTIMEOUTとCURLOPT_TIMEOUTの違い



Difference Between Php Curl Timeout Setting Curlopt_connecttimeout



PHP cURLには、CURLOPT_CONNECTTIMEOUTとCURLOPT_TIMEOUTの2つのタイムアウト設定があります。それらの違いは次のとおりです。

CURLOPT_CONNECTTIMEOUTは、サーバーに正常に接続するまでの待機時間をPHPに指示するために使用されます(接続が成功した後、出力のバッファリングを開始します)。このパラメーターは、ターゲットサーバーの過負荷、オフライン、クラッシュなどの考えられる状況に対処するためのものです。
CURLOPT_TIMEOUTは、サーバーからバッファーを受信するまで待機する時間を成功したPHPに通知するために使用されます。ターゲットが巨大なファイルの場合、コンテンツの生成速度が遅すぎる、またはリンク速度が遅すぎる場合、このパラメーターは次のようになります。これは便利です。
curlを使用してMP3ファイルをダウンロードすることは、開発者にとって良い例です。 CURLOPT_CONNECTTIMEOUTは10秒に設定できます。これは、サーバーが10秒以内に応答しない場合、スクリプトが切断されることを示します。CURLOPT_TIMEOUT100秒に設定できます。MP3ファイルが100秒以内にダウンロードされない場合、スクリプトは切断されます。



以下の点に注意してください。CURLOPT_TIMEOUTのデフォルトは0です。これは、リンクが切断されないことを意味します。したがって、設定しないと、リンクが遅すぎるためにHTTPリソースが使い果たされる可能性があります。

WordPressのwp_httpクラスでは、これら2つの値は同じであり、デフォルトは5秒に設定されています。



$ch = curl_init() curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE) curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE) curl_setopt($ch, CURLOPT_POST, 1) curl_setopt($ch, CURLOPT_POSTFIELDS, $content) curl_setopt($ch, CURLOPT_URL, $config['comet_server']. $api) //Requested api link curl_setopt($ch, CURLOPT_TIMEOUT, $timeout)//If the request is not successful within $timeout, the connection will be disconnected. The default is 0, generally set 5-10s $response = curl_exec($ch) curl_close($ch)

//最後に$ resposeはokを返します。これは、リクエストが成功したことを意味します。これを基準として、リクエストが成功したかどうかを判断できます。

$res = $response === 'OK' ? true : false

phpcurl関数には複数のパラメーターがあります。

CURLOPT_TIMEOUT_MSは、cURLの実行を許可される最大ミリ秒数を設定します。 cURL7.16.2で追加されました。 PHP5.2.3から入手できます。したがって、curl --versionを使用する場合は、最初にlibcurlのバージョンを確認してください。



しかし、この関数にはバグがあります。時間が1秒である1000ミリ秒未満の場合、エラーはすぐに報告されます。以下の説明を参照してください

If you want cURL to timeout in less than one second, you can use CURLOPT_TIMEOUT_MS, although there is a bug/'feature' on 'Unix-like systems' that causes libcurl to timeout immediately if the value is <1000 ms with the error 'cURL Error (28): Timeout was reached'. The explanation for this behavior is:
'If libcurl is built to use the standard system name resolver, that portion of the transfer will still use full-second resolution for timeouts with a minimum timeout allowed of one second.'
What this means to PHP developers is 'You can use this function without testing it first, because you can't tell if libcurl is using the standard system name resolver (but you can be pretty sure it is)'
The problem is that on (Li|U)nix, when libcurl uses the standard name resolver, a SIGALRM is raised during name resolution which libcurl thinks is the timeout alarm.
The solution is to disable signals using CURLOPT_NOSIGNAL. Here's an example script that requests itself causing a 10-second delay so you can test timeouts:

if(!isset($ _ GET ['foo'])){
//クライアント
$ 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エラー($ curl_errno):$ curl_error n '
} そうしないと {
echo '受信したデータ:$ data n'
}
} そうしないと {
//サーバー
sleep(10)
エコー「完了」。
}
?>

curl_setopt($ ch、CURLOPT_NOSIGNAL、1)を追加します