尝试获取请求标头cookie和响应cookie

时间:2019-07-24 23:33:01

标签: php curl

我正在尝试发送构建我的API,但是我必须先登录。 当我点击登录时,有响应头和请求头。他们设置了cookie,如图所示。 我如何才能将这些cookie放入变量以在其他文件或类似文件中使用?

reqested header responed header

我的代码

$file = 'cookies.txt';
$buffer = "__RequestVerificationToken=$ver;";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.xxxx.org/L/DoLogin");
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0');
curl_setopt($ch, CURLOPT_HEADER  ,array("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3"));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "__RequestVerificationToken=$buffer&username=xxxx&pw=xxxx&rememberMe=true&loginAcc=");

curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$content = curl_exec($ch);

// get cookies
$cookies = array();
preg_match_all('/Set-Cookie:(?<cookie>\s{0,}.*)$/im', $content, $cookies);

print_r($cookies['cookie']); // show harvested cookies

// basic parsing of cookie strings (just an example)
$cookieParts = array();
preg_match_all('/Set-Cookie:\s{0,}(?P<name>[^=]*)=(?P<value>[^;]*).*?expires=(?P<expires>[^;]*).*?path=(?P<path>[^;]*).*?domain=(?P<domain>[^\s;]*).*?$/im', $content, $cookieParts);
print_r($cookieParts);

0 个答案:

没有答案
相关问题