尝试使用PHP&登录网站卷曲?

时间:2010-03-17 15:27:49

标签: php curl login

我之前从未做过这样的事情......我正在尝试登录swagbucks.com并获取一些信息,但它无法正常工作。有人能告诉我我的剧本有什么问题吗?

<?php
$pages = array('home' =>
'http://swagbucks.com/?cmd=home',
           'login' =>
'http://swagbucks.com/?cmd=sb-login&from=/?cmd=home',
           'schedule' =>
'http://swagbucks.com/?cmd=sb-acct-account&display=2');
$ch = curl_init();
//Set options for curl session
$options = array(CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; `rv:1.9.2) Gecko/20100115 Firefox/3.6',`
         CURLOPT_HEADER => TRUE,
         //CURLOPT_RETURNTRANSFER => TRUE,
         CURLOPT_COOKIEFILE => 'cookie.txt',
         CURLOPT_COOKIEJAR => 'cookies.txt');


//Hit home page for session cookie
$options[CURLOPT_URL] = $pages['home'];
curl_setopt_array($ch, $options);
curl_exec($ch);

//Login
$options[CURLOPT_URL] = $pages['login'];
$options[CURLOPT_POST] = TRUE;
$options[CURLOPT_POSTFIELDS] = 'emailAddress=lala@yahoo.com&pswd=jblake&persist=on';
$options[CURLOPT_FOLLOWLOCATION] = FALSE;
curl_setopt_array($ch, $options);
curl_exec($ch);

//Hit schedule page
$options[CURLOPT_URL] = $pages['schedule'];
curl_setopt_array($ch, $options);
$schedule = curl_exec($ch);

//Output schedule
echo $schedule;

//Close curl session
curl_close($ch);
?> 

但它仍然没有让我登录。出了什么问题?

3 个答案:

答案 0 :(得分:1)

尝试回显每个请求以查看是否出现问题。 (启用CURLOPT_RETURNTRANSFER)

答案 1 :(得分:1)

我建议您使用

curl_setopt($ch, CURLOPT_COOKIEFILE, '/dev/null');

这样,cookie就可以内部存储在内存中而无需分离文件。

答案 2 :(得分:0)

对我来说,“persist = 1”,而不是“persist = on”:

$options[CURLOPT_POSTFIELDS] = 'emailAddress=lala@yahoo.com&pswd=jblake&persist=on'; // doesn't work
$options[CURLOPT_POSTFIELDS] = 'emailAddress=lala@yahoo.com&pswd=jblake&persist=1'; // works
$options[CURLOPT_POSTFIELDS] = 'emailAddress=lala@yahoo.com&pswd=jblake'; // also works