2017-01-24 93 views
0

我想创建一个php脚本,它登录到一个网站,然后导航到(可能多个)需要用户登录才能看到它们的网页。我想我已经成功使用cURL登录,但是我不知道如何在登录后“保持登录状态”来阅读文件内容。我怀疑我的问题与使用相同的cookie文件有关,但我不确定。这是我目前拥有的:PHP脚本在登录后读取html

<? 
//login form action url 
$url="https://randomwebsite.com/users/sign_in"; 
$postinfo = "utf8=%E2%9C%93&authenticity_token=ncgU2234iEMObg3334d3Iag%2BPBrmEerybFZ3X2fvVsUTmpjkPDQMgteeGlVDxFRfHjmHbvIYaTchvM2psKFzI%2BAHIpCw%3D%3D&user%5Botp_attempt%5D=step_1&user%5Blocale%5D=en&user%5Blogin%5D=randomuser&user%5Bpassword%5D=123456"; 

$cookie_file_path = "cookies1.txt"; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_NOBODY, false); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); 
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie_file_path); 

//set the cookie the site has for certain features, this is optional 
curl_setopt($ch, CURLOPT_COOKIE, "cookiename=0"); 
curl_setopt($ch, CURLOPT_USERAGENT, 
    "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_REFERER, $url); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); 

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo); 
$curlexecresponse = curl_exec($ch); 
//echo("\ncurlexecresponse: " . $curlexecresponse); 

// Now that we're logged in we can finally read the web pages we want to... 
$url2 = "url to members only page"; 
curl_setopt($ch, CURLOPT_URL, $url2); 
curl_setopt($ch, CURLOPT_POST, 0); 

$data = curl_exec($ch); 

echo("\n\n data is: " . $data); // this is empty 

curl_close($ch); 
?> 

如何在登录后在页面中读取HTML?

+0

看看这个帮助http://stackoverflow.com/questions/13020404/keeping-session-alive-with-curl-and -php – webDev

+0

如果切换到使用Guzzle,这可能会更容易。 – aalaap

回答

0

你需要登录后发送的每个请求的饼干英寸

$url2 = "url to members only page"; 
//Add this line 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); 
curl_setopt($ch, CURLOPT_URL, $url2); 
curl_setopt($ch, CURLOPT_POST, 0); 
+0

由于某种原因,即使添加了你提到的行,curl_exec()响应仍然会回到空白 – user2804881

+0

嘿,我注意到我的行中的卷曲对象是错误的,请使用$ ch而不是$ ch。我编辑了答案。 –

+0

如果还没有回来尝试“echo curl_error($ ch)”来查看是否有任何错误被抛出。 –