我正在尝试使用已保存的Cookie,而不是每次访问网站时都登录。
<?php
$uid = $_GET['uid'];
$ch = curl_init();
$cookie_file = dirname(__FILE__) . '/cookie.txt';
curl_setopt($ch, CURLOPT_URL, "http://www.forum.net/member.php?action=do_login");
curl_setopt($ch, CURLOPT_REFERER, "http://www.forum.net/member.php?action=login");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5");
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookie.txt');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, '/cookie.txt');
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=USERNAME&password=PASSWORD");
$exec = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "http://www.forum.net/reputation.php?uid=" . $uid . "&page=1");
$exec = curl_exec($ch);
echo $exec;
?>
由于某种原因,它不会让我重复使用cookie,而只是让我登录.cook.txt文件包含cookie的东西。
答案 0 :(得分:0)
你有:
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookie.txt');
...[snip]...
curl_setopt($ch, CURLOPT_COOKIEFILE, '/cookie.txt');
您正在将cookiefile的位置重置为文件系统的根目录,其中Web服务器进程几乎肯定无权编写文件。
同样,请确保网络服务器进程 CAN 在dirname(__FILE__)
指向的任何地方写入文件。
答案 1 :(得分:0)
您要同时设置CURLOPT_COOKIEJAR
和CURLOPT_COOKIEFILE
两次,最终结果将是不同的值。 '/cookie.txt'
将文件存储在系统根目录中,dirname(__FILE__) . '/cookie.txt'
将文件存储在与当前正在执行的脚本相同的目录中。确保您只使用相同的值(可能是后者)设置每个选项一次。
您也使用相同的句柄发出两个请求 - 这不是推荐的方法,并且可能在语义上不正确,因为当第二个应该是GET时,您将最终发布到两个页面。
我建议将cookie jar路径存储在块顶部的变量中,并将其传入。这样可以更容易地看到正在发生的事情。
我怀疑你想要更像这样的东西:
<?php
// Username and password for login
$username = 'USERNAME';
$password = 'PASSWORD';
// Create the target URLs
// Don't forget to escape your user input!
$loginUrl = "http://www.forum.net/member.php?action=do_login";
$reputationUrl = "http://www.forum.net/reputation.php?uid=" . urlencode($_GET['uid']) . "&page=1";
// Referer value for login request
$loginReferer = "http://www.forum.net/member.php?action=login";
// Note that __DIR__ can also be used in PHP 5.3+
$cookieJar = dirname(__FILE__) . '/cookie.txt';
// The User-Agent string to send
$userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5";
// Create the cURL handle for login
$ch = curl_init($loginUrl);
// Set connection meta
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// Set header-related options
curl_setopt($ch, CURLOPT_REFERER, $loginReferer);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieJar);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieJar);
// Set request body
$bodyFields = array(
'username' => $username,
'password' => $password
);
$body = http_build_query($bodyFields);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
// Execute the request
if (!curl_exec($ch)) {
exit("ERROR: Login request failed: " . curl_error($ch));
}
curl_close($ch);
// Create the cURL handle for reputation scrape
$ch = curl_init($reputationUrl);
// Set connection meta
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// Set header-related options
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieJar);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieJar);
// Execute the request
if (!$result = curl_exec($ch)) {
exit("ERROR: Reputation scrape request failed: " . curl_error($ch));
}
// Output the result
echo $result;