0

我正在使用 curl 登录网站,它设法让我登录并显示“感谢登录 Jack Brown”的页面,我可以看到会员区。

但是,我的服务器“/tmp/cookie.txt”上没有创建 cookie 文件

由于我已经登录,然后我想再次使用 curl 从成员区域检索数据,但是在运行页面的这一部分时,我只得到“请登录以继续”。

第一段代码用于登录(这可以让我登录,但不要创建 cookie 文件):

<?php

$email    = 'email@here.com';
$password = 'passwordhere';
$rememberMe = '';
$redirect = '';
// initial login page which redirects to correct sign in page, sets some cookies
$URL = 'https://www.website.co.uk/home';
$coookie = tempnam ("/tmp", "CURLCOOKIE");

$ch  = curl_init();

curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);        
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$page = curl_exec($ch);


$URL2 = "login url is normally here"; // this is our post url

$postdata = "_58_login=".$email."&_58_password=".$password."&_58_rememberMe=".$rememberMe."&_58_redirect=".$redirect; 

$post = substr($post, 0, -1);

// set additional curl options using our previous options
curl_setopt($ch, CURLOPT_URL, $URL2);
curl_setopt($ch, CURLOPT_REFERER, $URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);

$page = curl_exec($ch); // make request

// try to find the actual login form
if (!preg_match('/Thanks for loggin in/is', $page, $form)) {
    die('Erorr: Could not login!');
}
var_dump($page); // should be logged in

// END OF LOGIN

下一段代码在同一个文档中,用于在成员区域打开另一个页面,我可以从中提取内容,但是这个页面只是返回说我没有登录:

$URLOPEN = 'http://www.website.co.uk/membersareacontent';
$URL5 = 'http://www.website.co.uk/thanksforlogginin';


curl_setopt($ch, CURLOPT_URL, $URLOPEN);
curl_setopt($ch, CURLOPT_REFERER, $URL5);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);        
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$page2 = curl_exec($ch);
var_dump($page2); // should be show members content
4

2 回答 2

0

可能是因为变量名称在顶部声明时有一个额外的名称,o然后是您稍后在 cURL 中使用的变量名称:

$coookie = tempnam ("/tmp", "CURLCOOKIE"); // Notice coookie with the extra o

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); // Spelled with only two o's here
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);  // Spelled with only two o's here
于 2012-12-14T21:24:02.340 回答
0

编辑:sbeliv01 指出 cookie 拼写错误如果不尝试将其添加到代码顶部以显示是否有任何错误:

error_reporting(E_ALL);
ini_set("display_errors",1);

此代码用于检查文件是否可写入,并确保 cookie 文件可写入:

// change $cookie_file_path to yours
$fp = fopen($cookie_file_path,'wb') or die("can't open cookie file");
fclose($fp);

我还注意到你没有在这里设置你的 cookie 选项:

// set additional curl options using our previous options
$ch  = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL2);
curl_setopt($ch, CURLOPT_REFERER, $URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);        
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
于 2012-12-14T21:23:11.710 回答