php curl 设置cookie 我的需求是访问a页面 a curl b页面因为b页面中要设置cookie我想访问 a页面 能将b页面设置的cookie设置下来有没有高手解决下啊 谢谢了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用curl 读取一个页面 并且把cookie保存到客户端 function curlFetch($url, $cookie = "", $referer = "", $data = null) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回字符串,而非直接输出 curl_setopt($ch, CURLOPT_HEADER, false); // 不返回header部分 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); // 设置socket连接超时时间 if (!empty($referer)) { curl_setopt($ch, CURLOPT_REFERER, $referer); // 设置引用网址 } if (!empty($cookie)) { curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); // 设置从$cookie所指文件中读取cookie信息以发送 curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); // 设置将返回的cookie保存到$cookie所指文件 } if (is_null($data)) { // GET } else if (is_string($data)) { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // POST } else if (is_array($data)) { // POST curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); } set_time_limit(120); // 设置自己服务器超时时间 $str = curl_exec($ch); curl_close($ch); return $str; 谢谢五楼的回答 我是想吧抓取过来页面的cookie同时保存到客户端 你的代码只是保存在了 服务器端 就是说客户端c访问页面b b用curl抓取的a b将a的cookie保存到c客户端 呵呵,和我以前遇到的问题一模一样,我理解你的意思,,,,我采集那个游戏的时候也是这样,,后来我就直接把cookie通过不同的名字保存到了服务器端!!解决了问题,,,你说的你要保存到客户端,给你一个办法!!if($this->_iscookie==false){//获取cookie存入本机cookie preg_match_all('|Set-Cookie: (.*);|U', $this->_source, $results); $cookies = implode(';', $results[1]); setcookie("cookname", $cookies, time()+3600*24*7,"/"); } 谢谢了 结决了 贴上代码 让大家看看$ch = curl_init('http://....'); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $c=curl_exec($ch); $arr=explode('<!--ppd_content-->',$c); preg_match_all('|Set-Cookie: (.*);|U', $c, $results); foreach($results[1] as $ck) { $u=explode('=',$ck); if(!empty($u[0]) and !empty($u[1]) and $u[1]!='deleted') { setcookie($u[0],rawurldecode($u[1]),0,'/','.brothersoft.com'); } } echo $arr[1]; curl_close($ch); 又出现新问题了 请高手继续帮帮忙 还是上面的东西 如果通过curl发送cookie 不要用保存在服务器文件里的方法 考虑到并发 必须将 cookie发送出去 我用了curl_setopt($ch, CURLOPT_COOKIE, 'zwl=nihao;');貌似不可以 网站合成 求PHP把字体转换为图片. 四数求24 谁能出个好招呢? 急,请问如何将以下分页程序(已经测试成功)修改为smarty形式,急急急 php 短信接口源码,虽然简单,还是实用的.分享一下 如何使用VPN访问PHP+Mysql开发的系统 php 字符串变短 环境不支持的函数 appache 中设置自定义404、403错误 现在比较好的php免费空间 都有哪些啊 谢谢 谁用过 fastdfs
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回字符串,而非直接输出
curl_setopt($ch, CURLOPT_HEADER, false); // 不返回header部分
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); // 设置socket连接超时时间
if (!empty($referer))
{
curl_setopt($ch, CURLOPT_REFERER, $referer); // 设置引用网址
}
if (!empty($cookie))
{
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); // 设置从$cookie所指文件中读取cookie信息以发送
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); // 设置将返回的cookie保存到$cookie所指文件
} if (is_null($data))
{
// GET
}
else if (is_string($data))
{
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// POST
}
else if (is_array($data))
{
// POST
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
}
set_time_limit(120); // 设置自己服务器超时时间
$str = curl_exec($ch);
curl_close($ch);
return $str;
就是说客户端c访问页面b
b用curl抓取的a
b将a的cookie保存到c客户端
preg_match_all('|Set-Cookie: (.*);|U', $this->_source, $results);
$cookies = implode(';', $results[1]);
setcookie("cookname", $cookies, time()+3600*24*7,"/");
}
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$c=curl_exec($ch);
$arr=explode('<!--ppd_content-->',$c);
preg_match_all('|Set-Cookie: (.*);|U', $c, $results);
foreach($results[1] as $ck)
{
$u=explode('=',$ck);
if(!empty($u[0]) and !empty($u[1]) and $u[1]!='deleted')
{
setcookie($u[0],rawurldecode($u[1]),0,'/','.brothersoft.com');
}
}
echo $arr[1];
curl_close($ch);
我用了
curl_setopt($ch, CURLOPT_COOKIE, 'zwl=nihao;');
貌似不可以