2013-07-08 68 views
0

我试图让它使用保存的cookie,而不是每次访问该站点时都记录。重复使用cookie cURL PHP

<?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,出于某种原因,而是它只是记录我该cookie.txt文件中包含该cookie的东西。

+1

您同时设置'CURLOPT_COOKIEJAR'和'CURLOPT_COOKIEFILE'两次,而最终的结果将是不同的值。 '/ cookie.txt'将把文件存储在系统根目录下,并且'dirname(__ FILE__)。 '/ cookie.txt'会将文件存储在与当前正在执行的脚本相同的目录中。确保你只设置每个选项一次,具有相同的值(可能是后者)。 – DaveRandom

+0

好吧,我将CURLOPT_COOKIEJAR和CURLOPT_COOKIEFILE设置为:dirname(__ FILE__)。 '/cookie.txt' - 但是,它不会工作。用新代码更新OP。 – MortenMoulder

+0

可能的重复http://stackoverflow.com/questions/12885538/php-curl-and-cookies/ – ChrisF

回答

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__)指向的任何位置。

0

你设置这两个CURLOPT_COOKIEJARCURLOPT_COOKIEFILE两次,而最终的结果将是不同的值。 '/cookie.txt'会将该文件存储在系统根目录中,并且dirname(__FILE__) . '/cookie.txt'将将该文件存储在与当前正在执行的脚本相同的目录中。确保你只设置每个选项一次,具有相同的值(可能是后者)。

您也使用相同的句柄发出两个请求 - 这不是推荐的方法,并且可能在语义上不正确,因为当第二个页面应该是GET时,您最终会将POST张贴到这两个页面。

我建议将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; 
+0

非常感谢组织我的代码。那真的很好看!然而...该代码给了我这个错误:解析错误:语法错误,意外的'“错误:登录请求失败:'(T_CONSTANT_ENCAPSED_STRING)在线48上的C:\ myfile.php – MortenMoulder

+0

@Snorlax修正我总是忘记' exit''需要大括号,不像'echo'和'print' – DaveRandom

+0

好了,它现在可以工作,但它仍然不使用cookie文件,每次访问文件时都会登录,并且是,制作cookie文件,并且正在制作。 – MortenMoulder