PHP CURL 使用 preg_match 解析多个 set-cookie 标头
我正在使用 PHP/CURL 来自动执行两个紧密相连的代码点火器之间的调用。
代码点火器返回两个 set-cookie
标头,一个用于包含真实会话数据的安全 cookie,一个用于与空会话的不安全连接...
Set-Cookie: overshare=a%3A0%3A%7B%7D; expires=Thu, 17-Jun-2010 05:09:32 GMT; path=/
Set-Cookie: overshare=BdHJPVt...STsCxnMBj; path=/; secure
我一直在尝试解析安全 cookie (两个站点都在同一个域上,所以如果我通过 CURL 获取更新的会话信息,我应该更新客户端 cookie,就像他们直接进行调用一样)
我当前正在使用以下内容来解析 cookie:
preg_match('/Set-Cookie: (.*)\b/', $Head, $Cookies);
这给了我 $Cookies
:
Array
(
[0] => Set-Cookie: overshare=a%3A0%3A%7B%7D; expires=Thu, 17-Jun-2010 05:09:32 GMT; path
[1] => overshare=a%3A0%3A%7B%7D; expires=Thu, 17-Jun-2010 05:09:32 GMT; path
)
但这是仅匹配第一个 set-cookie 标头。我的正则表达式技能很差 - 如何匹配第二个标头?
I'm using PHP/CURL to automate calls between 2 closely tied code igniter.
Code igniter is returning two set-cookie
headers, one for a secure cookie with the real session data, one for insecure connections with an empty session...
Set-Cookie: overshare=a%3A0%3A%7B%7D; expires=Thu, 17-Jun-2010 05:09:32 GMT; path=/
Set-Cookie: overshare=BdHJPVt...STsCxnMBj; path=/; secure
I've been trying to parse the secure cookie (both sites are on the same domain so if I get updated session information via CURL, I should update the clients cookie as if they made the call directly)
I'm currently using the following to parse the cookie:
preg_match('/Set-Cookie: (.*)\b/', $Head, $Cookies);
which gives me in $Cookies
:
Array
(
[0] => Set-Cookie: overshare=a%3A0%3A%7B%7D; expires=Thu, 17-Jun-2010 05:09:32 GMT; path
[1] => overshare=a%3A0%3A%7B%7D; expires=Thu, 17-Jun-2010 05:09:32 GMT; path
)
but this is only matching the first set-cookie header. My regex skills are poor - how can I match the second header?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设
$Head
是包含所有 cookie 标头的单个字符串,您正在寻找preg_match_all()
。
preg_match()
在找到第一个匹配项后停止。使用
preg_match_all()
,匹配的整个字符串将位于$Cookies[0]
中。您的子模式匹配将位于$Cookies[1]
中。另外,默认
情况下,通配符
(.*)
是贪婪的,因此如果标头不在不同的行上,它可能会一起消耗两个字符串。如果是这样,请尝试(.*?)
使其变得不贪婪。Assuming
$Head
is a single string containing all of the cookie headers, you're looking forpreg_match_all()
.preg_match()
stops after finding the first match.With
preg_match_all()
, matched entire strings will be in$Cookies[0]
. Your subpattern matches will be in$Cookies[1]
.yields
Also, your wildcard
(.*)
is greedy by default, so it may consume both strings together if the headers aren't on separate lines. If so, try(.*?)
to make it ungreedy.