简体   繁体   English

PHP-CURL-Set-Cookie标头(忽略点)

[英]PHP - CURL - Set-Cookie Header (Ignoring dots)

Im trying to fetch the Set-Cookie value by logging in to an external site and later using this value to perform different actions. 我试图通过登录到外部站点并稍后使用此值执行不同的操作来获取Set-Cookie值。 I can not save it with CookieJar, I need to return the string from a function. 我无法使用CookieJar保存它,我需要从函数返回字符串。

Anyway. 无论如何。 Expected Cookie should look like this : "JSESSIONID=123.cls_rs2a_8009" but I am getting "JESSIONID=123" 预期的Cookie应该如下所示:“ JSESSIONID = 123.cls_rs2a_8009”,但我却收到了“ JESSIONID = 123”

So it seems to ignore everything after the dot - OR its not receiving it the same way like a browser does. 因此,它似乎忽略了点之后的所有内容-或它没有像浏览器一样接收到它。

Any help on this? 有什么帮助吗?

Current code example not showing the URL : 当前代码示例未显示网址:

$headers = [];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADERFUNCTION,
    function($curl, $header) use (&$headers){
        $len = strlen($header);
        $header = explode(':', $header, 2);
        if (count($header) < 2)
             return $len;

        $name = strtolower(trim($header[0]));
        if (!array_key_exists($name, $headers))
            $headers[$name] = [trim($header[1])];
        else
            $headers[$name][] = trim($header[1]);
        return $len;
    }
);

$data = curl_exec($ch);
var_dump($headers);

Probably using Guzzle should be a little easier. 可能使用Guzzle应该更容易一些。 From Guzzle Docs : Guzzle Docs

// Use a specific cookie jar
$jar = new \GuzzleHttp\Cookie\CookieJar;
$r = $client->request('GET', 'http://httpbin.org/cookies', [
    'cookies' => $jar
]);

Fro your example, it seems that you're missing the headers in response : 在您的示例中,似乎您缺少响应头

// get headers too with this line
curl_setopt($ch, CURLOPT_HEADER, 1);

You can also try to get the full cookies list : 您也可以尝试获取完整的Cookie列表

curl_getinfo($curl, CURLINFO_COOKIELIST);

This migth also come handy: Guzzle cookies handling 这种麻烦也派上用场: 食糖饼干处理

Hope that helps. 希望能有所帮助。

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

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