curl_share_setopt

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

curl_share_setopt為 cURL 共享句柄設置選項。

說明

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

為給定的 cURL 共享句柄設置一個選項。

參數

sh

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

option

Option Description
CURLSHOPT_SHARE 指定要共享的數據類型。
CURLSHOPT_UNSHARE 指定不再共享的數據類型。

value

Value Description
CURL_LOCK_DATA_COOKIE 共享 cookie 數據。
CURL_LOCK_DATA_DNS 共享 DNS 緩存。注意,當你使用 cURL 多句柄時,默認所有添加在同一個多句柄的句柄都將會共享 DNS 緩存。
CURL_LOCK_DATA_SSL_SESSION 共享 SSL 的 session ID, 在重連同樣的服務器時減少 SSL 握手時間。注意,SSL 的 session ID 在同一個的句柄中默認是重復使用的。

返回值

成功時返回 true, 或者在失敗時返回 false。

范例

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

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

<?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);
?>