我的需求是
访问a页面  a curl b页面
因为b页面中要设置cookie
我想访问 a页面 能将b页面设置的cookie设置下来
有没有高手解决下啊  谢谢了

解决方案 »

  1.   

    用curl 读取一个页面  并且把cookie保存到客户端
      

  2.   

    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;
      

  3.   

    谢谢五楼的回答  我是想吧抓取过来页面的cookie同时保存到客户端  你的代码只是保存在了 服务器端
      就是说客户端c访问页面b 
      b用curl抓取的a 
      b将a的cookie保存到c客户端
      

  4.   

    呵呵,和我以前遇到的问题一模一样,我理解你的意思,,,,我采集那个游戏的时候也是这样,,后来我就直接把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,"/");
    }
      

  5.   

    谢谢了 结决了 贴上代码 让大家看看$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);
      

  6.   

    又出现新问题了 请高手继续帮帮忙 还是上面的东西 如果通过curl发送cookie  不要用保存在服务器文件里的方法 考虑到并发 必须将 cookie发送出去 
    我用了
    curl_setopt($ch, CURLOPT_COOKIE, 'zwl=nihao;');
    貌似不可以