在 PHP CURL 中设置 cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27958072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:38:25  来源:igfitidea点击:

setting a cookie in PHP CURL

phpcurlcookies

提问by Sho Gum Lew

Im really new to the php curl concept, can anybody show me a simple example of how to set a cookie in the browser using php curl

我对 php curl 概念真的很陌生,任何人都可以向我展示如何使用php curl在浏览器中设置 cookie 的简单示例

this is my code that doesnt work...

这是我的代码不起作用...

$ch = curl_init('http://localhost/setc.php?userid=123&panelid=1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// get headers too with this line
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_exec($ch);
curl_close($ch);

but when i take a var_dump of this it gets printed, very confused please help

但是当我使用 var_dump 时它会被打印出来,非常困惑,请帮忙

array (size=1)
  'userid' => string '1:123' (length=8)

please if ur confused....u can simply ignore my above coding and let me know how to set a simple cookie using php curl

请如果你感到困惑....你可以简单地忽略我上面的编码,让我知道如何使用 php curl 设置一个简单的 cookie

回答by gview

I'm not clear on what you are expecting to have happen with cookies. However, you might try this:

我不清楚您对 cookie 的期望。不过,你可以试试这个:

$ch = curl_init('http://localhost/setc.php?userid=123&panelid=1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// get headers too with this line
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIE, 'cookiename=cookievalue');
curl_exec($ch);
curl_close($ch);

回答by Yash

Try this:

尝试这个:

curl_setopt($ch , CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch , CURLOPT_COOKIEFILE, 'cookies.txt');

This will automatically create one cookies.txt

这将自动创建一个 cookies.txt

回答by Rohan

Php Curl allows you to directly set the cookie Header, using:

Php Curl 允许你直接设置 cookie Header,使用:

curl_setopt($curlHandle,CURLOPT_COOKIE, $cookieString);

Where $cookieString holds semicolon separated key=value pairs of cookie data.

$ cookieStrin摹持有分号分隔的cookie数据的键值对。

Please refer to this question: PHP Curl and setcookie problem

请参考这个问题: PHP Curl 和 setcookie 问题

And be sure to thoroughly go through http://php.net/manual/en/function.curl-setopt.php

并且一定要彻底浏览http://php.net/manual/en/function.curl-setopt.php