[英]Getting cookies with PHP curl
I'm trying to get the cookies send by super simple script using the curl
.我正在尝试使用curl通过超级简单的脚本发送
curl
。 Here are the files:以下是文件:
get_cookie.php: get_cookie.php:
...
$curl = curl_init("http://.../set_cookie.php");
$data = array('fname'=>"John", 'lname'=>"Brown");
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
...
$response = curl_exec($curl);
...
// get cookies
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $response, $matches);
$cookies = array();
foreach($matches[1] as $item) {
parse_str($item, $cookie);
$cookies = array_merge($cookies, $cookie);
}
echo "cookies recived: <br>";
var_dump($cookies);
set_cookie.php: set_cookie.php:
...
if (isset($_POST["fname"])) {
setcookie("usr_name", "user_".$_POST["fname"], time()+60*60);
...
}
but on the curl response i get no cookies - the $cookies
array is empty (I've checked - the $_POST["fname"]
is set - the POST data was sent successfully ).但是在 curl 响应中,我没有得到 cookies -
$cookies
数组是空的(我已经检查过 - 设置了$_POST["fname"]
- POST 数据已成功发送)。 When i call the set_cookie
it the standard way - from the browser i get the desired usr_name cookie.当我以标准方式调用
set_cookie
时 - 从浏览器中我得到所需的usr_name cookie。 What's the problem with curl
? curl
有什么问题?
Have you tried curl_getinfo($curl, CURLINFO_COOKIELIST)
instead of parsing manually the response to get cookies?您是否尝试过
curl_getinfo($curl, CURLINFO_COOKIELIST)
而不是手动解析响应以获取 cookies?
see https://curl.haxx.se/docs/http-cookies.html见https://curl.haxx.se/docs/http-cookies.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.