0

我有这个代码

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.web.com/index.php?q=login");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            "nick=abc&pass=abc");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec ($ch);


print_r($server_output);

在这个地址 www.web.com/index.php?q=login 是一个登录表单。我试图在那里登录但没有任何成功。($server_output仍然只包含登录表单 - 它应该包含用户面板 - 成功登录后)

4

1 回答 1

0

您需要执行其他操作,例如接受 cookie,这里有一些代码可用于初始化 curl。

$this->_ch = curl_init();

    curl_setopt($this->_ch, CURLOPT_VERBOSE, 0);
    curl_setopt($this->_ch, CURLOPT_HEADER, 0);
    curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($this->_ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($this->_ch, CURLOPT_TIMEOUT,1000);
    curl_setopt($this->_ch, CURLOPT_COOKIEFILE, "cookies.txt");
    curl_setopt($this->_ch, CURLOPT_COOKIEJAR, "cookies.txt");
    curl_setopt($this->_ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
于 2013-06-29T09:18:00.777 回答