CURL保持与get和post的会话

时间:2013-05-09 13:32:49

标签: php curl

我已经登录了一个有帖子的网站,没关系。当我用GET方法得到这个网站时,一切都很好(回复“你好,用户名!”等),但当我试图发送帖子到这个网站时,它将我重定向到登录页面。似乎卷曲不会发送使用post方法的cookie会话。怎么解决? 我的代码:

public function curlInit()
{
    $this->_ch = null;
    $this->_ch = curl_init();
    curl_setopt($this->_ch, CURLOPT_USERAGENT, $this->_userAgent);
    curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($this->_ch, CURLOPT_CONNECTTIMEOUT, 25);
    curl_setopt($this->_ch, CURLOPT_VERBOSE, 1);
    curl_setopt($this->_ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookies-jar.txt');
    curl_setopt($this->_ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookies-jar.txt');
    curl_setopt($this->_ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($this->_ch, CURLOPT_COOKIESESSION, 1);
    curl_setopt($this->_ch, CURLOPT_HTTPHEADER, 1);
    curl_setopt($this->_ch, CURLOPT_AUTOREFERER,  1);
    if ($this->proxy)
    {
        curl_setopt ($this->_ch, CURLOPT_PROXY, $this->proxy);
    }
}
public function getPostSite($url, $data)
{
    $this->curlError = false;

    curl_setopt($this->_ch,CURLOPT_URL, $url);
    curl_setopt($this->_ch,CURLOPT_POST, 1);
    curl_setopt($this->_ch,CURLOPT_POSTFIELDS, $data);
    $result = curl_exec($this->_ch);
    if (curl_errno($this->_ch))
    {
        $this->curlError = curl_errno($this->_ch);
        return false;
    }
    return $result;
}
public function  getGetSite($url)
{
    curl_setopt($this->_ch,CURLOPT_POST, 0);
    curl_setopt($this->_ch, CURLOPT_URL, $url);
    $data = curl_exec($this->_ch);
    return $data;
} 

0 个答案:

没有答案