curl_share_setopt

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

curl_share_setopt為 cURL 共享句柄設(shè)置選項(xiàng)。

說明

curl_share_setopt(resource $sh, int $option, string $value): bool

為給定的 cURL 共享句柄設(shè)置一個(gè)選項(xiàng)。

參數(shù)

sh

一個(gè)由curl_share_init()函數(shù)返回的 cURL 共享句柄。

option

Option Description
CURLSHOPT_SHARE 指定要共享的數(shù)據(jù)類型。
CURLSHOPT_UNSHARE 指定不再共享的數(shù)據(jù)類型。

value

Value Description
CURL_LOCK_DATA_COOKIE 共享 cookie 數(shù)據(jù)。
CURL_LOCK_DATA_DNS 共享 DNS 緩存。注意,當(dāng)你使用 cURL 多句柄時(shí),默認(rèn)所有添加在同一個(gè)多句柄的句柄都將會(huì)共享 DNS 緩存。
CURL_LOCK_DATA_SSL_SESSION 共享 SSL 的 session ID, 在重連同樣的服務(wù)器時(shí)減少 SSL 握手時(shí)間。注意,SSL 的 session ID 在同一個(gè)的句柄中默認(rèn)是重復(fù)使用的。

返回值

成功時(shí)返回 true, 或者在失敗時(shí)返回 false。

范例

示例 #1 curl_share_setopt() 函數(shù)的范例:

以下范例將會(huì)創(chuàng)建一個(gè) cURL 共享句柄,并且往其中添加兩個(gè) cURL 句柄,最后共享這兩個(gè) cURL 句柄的 cookie 數(shù)據(jù)運(yùn)行。

<?php
// Create cURL share handle and set it to share cookie data
$sh curl_share_init();
curl_share_setopt($shCURLSHOPT_SHARECURL_LOCK_DATA_COOKIE);

// Initialize the first cURL handle and assign the share handle to it
$ch1 curl_init("http://example.com/");
curl_setopt($ch1CURLOPT_SHARE$sh);

// Execute the first cURL handle
curl_exec($ch1);

// Initialize the second cURL handle and assign the share handle to it
$ch2 curl_init("http://php.net/");
curl_setopt($ch2CURLOPT_SHARE$sh);

// Execute the second cURL handle
//  all cookies from $ch1 handle are shared with $ch2 handle
curl_exec($ch2);

// Close the cURL share handle
curl_share_close($sh);

// Close the cURL handles
curl_close($ch1);
curl_close($ch2);
?>