我正在做一个班级项目,我们需要使用 php 来编写一个网站。我们被告知应该编写单独的页面来查询信息以及用于保存变量的 php 会话。我已经成功启动了一个会话,但是当我尝试发出请求并为会话添加标头时,请求会停止并且无法完成。下面是我正在使用的代码。
function GetDataV2(string $URL, string $method, array $postPayload)
{
$sessID = $_COOKIE['PHPSESSID'];
$cookieString = "Cookie: PHPSESSID=$sessID";
$options = array(
'http' => array(
'header' => array('Content-Type: application/x-www-form-urlencoded', 'Accept: application/json', $cookieString),
'method' => 'POST',
'content' => http_build_query($postPayload)
)
);
$context = stream_context_create($options);
return file_get_contents($URL, false, $context);
}
感谢任何帮助,因为我似乎无法弄清楚是什么导致请求挂起。不幸的是,该项目需要 php,否则我根本不会使用它。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
在进行更广泛的搜索后,我需要在执行调用之前添加 session_write_close() 以释放会话文件,然后其他页面才能修改它。