简体   繁体   English

如何使用PHP在CURL请求中获取Cookies?

[英]How to get Cookies in CURL request using PHP?

I have two files: A.php and B.php. 我有两个文件:A.php和B.php。

Contents of A.php: A.php的内容:

<?php
$ch = curl_init();
curlsetopt($ch,CURLOPT_URL,'localhost/b.php');
curl_exec($ch);
?>

Contents of B.php: B.php的内容:

<?php
print_r($_COOKIE);
?>

it isn't printing COOKIES when loading A.php but printing when loading b.php directly.please help thanks 加载A.php时不是打印COOKIES而是直接加载b.php时打印。请帮助谢谢

cURL requests don't send cookies by default. 默认情况下,cURL请求不发送cookie。 If you want to pass all of the $_COOKIE s from script a.php to b.php do this: 如果要将所有$_COOKIE从脚本a.php传递到b.php,请执行以下操作:

<?php

$cookie = array();

foreach ($_COOKIE as $key => $value) {
    $cookie[] = "{$key}={$value}";
};

$cookie = implode('; ', $cookie);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'localhost/b.php');
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_exec($ch);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM