我在做什么错? CURL PHP Cookies

问题描述:

我正在使用curl登录到网站,它设法登录并显示回页面说“感谢登录杰克布朗”,我可以看到会员区。我在做什么错? CURL PHP Cookies

但是一个cookie文件不被我的服务器上创建的“/tmp/cookie.txt”

自从我登录,我再想卷曲再次来撷取会员区的数据,但在运行时页面的这一部分,我只是得到“请登录继续”。

代码的第一位是用于登录(这会将我还可以,但不创建一个cookie文件):

<?php 

$email = '[email protected]'; 
$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

添加'curl_setopt($ ch,CURLOPT_AUTOREFERER,TRUE);'并更新您的文章,并提供有关现在问题的最新更改。我在评论中看到,您已经做出了比这里描述的更进一步的答案,但仍需要帮助 – Ranty

编辑: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); 
+0

添加了大量代码并更正了Cookie名称,但仍然处于相同的情况,没有关于该Cookie可被写入的错误代码。 –

+0

我注意到你没有CURLOPT_COOKIEJAR选项,当请求被发送后。 – user969068

+0

我已经添加了这些内容,但仍然没有任何区别:(感谢您迄今为止的帮助! –

也许是因为变量名称在顶部声明时还有一个额外的o,那么稍后将在您的卷曲库中使用该变量:

$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 
+0

谢谢,我改变了这一点,但仍然有同样的问题。 –