2012-12-14 40 views
0

我正在使用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

回答

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

添加了大量代碼並更正了Cookie名稱,但仍然處於相同的情況,沒有關於該Cookie可被寫入的錯誤代碼。 –

+0

我注意到你沒有CURLOPT_COOKIEJAR選項,當請求被髮送後。 – user969068

+0

我已經添加了這些內容,但仍然沒有任何區別:(感謝您迄今爲止的幫助! –

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

謝謝,我改變了這一點,但仍然有同樣的問題。 –