PHP CURL: 使用变量设置 HTTP Cookie 标头
您可以将 Cookie 存储在变量中,并将其添加到 HTTP 标头中,如以下示例所示:
// 定义 Cookie 变量
$cookie = 'session_id=abc123; user_id=123';
// 设置 HTTP 标头
$headers = array(
'Content-Type: application/json',
'Cookie: ' . $cookie
);
// 发送 HTTP 请求
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://example.com/api');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
在上面的示例中,我们首先定义了一个名为 $cookie 的变量,其中包含要发送的 Cookie 数据。然后,我们定义了一个名为 $headers 的变量,其中包含要发送的 HTTP 标头,包括 Cookie 标头,使用 $cookie 变量中存储的数据。最后,我们使用 curl_setopt() 函数设置了 CURLOPT_HTTPHEADER 选项来发送 HTTP 标头,并使用 curl_exec() 函数发送 HTTP 请求。
原文地址: https://www.cveoy.top/t/topic/oxUu 著作权归作者所有。请勿转载和采集!