我已经用帖子登录了一个网站,没关系。当我使用 GET 方法获取该站点时,一切正常(返回“您好,用户名!”等),但是当我尝试向该站点发送帖子时,它会将我重定向到登录页面。似乎 curl 不会使用 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;
}