我正在使用curl登录网站,它设法登录并显示页面回复说“感谢登录杰克布朗”,我可以看到会员区。
但是我的服务器上没有创建cookie文件“/tmp/cookie.txt”
由于我已登录,因此我想再次使用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
答案 0 :(得分: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);
答案 1 :(得分: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