PHP 8.1.28 Released!

curl_share_init

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

curl_share_init初始化 cURL 共享句柄

说明

curl_share_init(): CurlShareHandle

允许在 cURL 句柄之间共享数据。

参数

此函数没有参数。

返回值

返回 cURL 共享句柄。

更新日志

版本 说明
8.0.0 此函数现在返回 CurlShareHandle 实例,之前返回 resource

示例

示例 #1 curl_share_init() 示例

以下示例将会创建 cURL 共享句柄,并且往其中添加两个 cURL 句柄,最后用共享的 cookie 数据运行它们。

<?php
// 创建 cURL 共享句柄,并设置共享 cookie 数据
$sh = curl_share_init();
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);

// 初始化第一个 cURL 句柄,并将它设置到共享句柄
$ch1 = curl_init("http://example.com/");
curl_setopt($ch1, CURLOPT_SHARE, $sh);

// 执行第一个 cURL 句柄
curl_exec($ch1);

// 初始化第二个 cURL 句柄,并将它设置到共享句柄
$ch2 = curl_init("http://php.net/");
curl_setopt($ch2, CURLOPT_SHARE, $sh);

// 执行第二个 cURL 句柄
// all cookies from $ch1 handle are shared with $ch2 handle
curl_exec($ch2);

// 关闭 cURL 共享句柄
curl_share_close($sh);

// 关闭 cURL 共享句柄
curl_close($ch1);
curl_close($ch2);
?>

参见

add a note

User Contributed Notes 1 note

up
2
Robert Chapin
6 years ago
Cookie handling is DISABLED by default.  The following must be used prior to CURLOPT_SHARE.

curl_setopt($ch1, CURLOPT_COOKIEFILE, "");
curl_setopt($ch2, CURLOPT_COOKIEFILE, "");

Also, do not attempt to use CURLOPT_SHARE with curl_setopt_array because this can cause the options to be set in the wrong order, which will fail.
To Top