如何获取cookie从php curl到变量 [英] how to get the cookies from a php curl into a variable

查看:388
本文介绍了如何获取cookie从php curl到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以一些其他公司的人认为如果不使用soap或xml-rpc或休息或任何其他合理的通信协议,他只是嵌入他的所有的响应作为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.

我需要将这些cookie拉出来,希望这个curl响应的数组。如果我不得不浪费一大堆我的生活写一个解析器,我会非常不开心。

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);

这篇关于如何获取cookie从php curl到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆