问题描述
所以其他公司的一些人认为,如果他不使用soap、xml-rpc 或rest 或任何其他合理的通信协议,而是将他的所有响应作为cookie 嵌入到标头中,那就太棒了.
So some guy at some other company thought it would be awesome if instead of using soap or xml-rpc or rest or any other reasonable communication protocol he just embedded all of his response as cookies in the header.
我需要从这个 curl 响应中取出这些 cookie 作为一个数组.如果我不得不为此浪费大量生命来编写解析器,我会很不高兴.
I need to pull these cookies out as hopefully an array from this curl response. If I have to waste a bunch of my life writing a parser for this I will be very unhappy.
有谁知道如何简单地做到这一点,最好不要向文件写入任何内容?
Does anyone know how this can simply be done, preferably without writing anything to a file?
如果有人能帮我解决这个问题,我将不胜感激.
I will be very grateful if anyone can help me out with this.
推荐答案
$ch = curl_init('http://www.google.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// get headers too with this line
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
// get cookie
// multi-cookie variant contributed by @Combuster in comments
preg_match_all('/^Set-Cookie:s*([^;]*)/mi', $result, $matches);
$cookies = array();
foreach($matches[1] as $item) {
parse_str($item, $cookie);
$cookies = array_merge($cookies, $cookie);
}
var_dump($cookies);
这篇关于如何将 php curl 中的 cookie 转换为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!