You are setting both CURLOPT_COOKIEJAR
and CURLOPT_COOKIEFILE
twice, and the final result will be different values. '/cookie.txt'
will store the file in the system root, and dirname(__FILE__) . '/cookie.txt'
will store the file in the same directory as the currently executing script. Ensure you are only setting each option once, with the same value (probably the latter).
You are also making two requests with the same handle - this is not the recommended approach, and is likely semantically incorrect, because you will end up POSTing to both pages, when the second should probably be a GET.
I recommend storing the cookie jar path in a variable at the top of the block, and passing this in. This makes it easier to see what is going on.
I suspect you want something more like this:
<?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;