开发者

Logging into a PHP forum using lib-cURL and getting a page contents

开发者 https://www.devze.com 2022-12-15 07:22 出处:网络
This is my code: $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, \"http://www.membersite.com/login.php\");

This is my code:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://www.membersite.com/login.php");
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'username=deleted&password=deleted');
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'C:\xampp\htdocs\scrape\cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$store = curl_exec ($ch);

curl_setopt($ch, CURLOPT_URL, "http://www.membersite.com/memberlist.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$pa开发者_开发知识库ge = curl_exec ($ch);

echo $page;

curl_close($ch);

But I don't think it's successfully logging in as the site (my own, by the way) doesn't show a log of me signing in. I know the username and password are correct, as are the URLs. I get a cookie.txt file back with what looks like good data but I'm not sure.

If I try some basic debugging, like this:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://www.membersite.com/login.php");
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'username=deleted&password=deleted');
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'C:\xampp\htdocs\scrape\cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

if(!$store = curl_exec($ch))
{
    echo "login fail";
}

curl_setopt($ch, CURLOPT_URL, "http://www.membersite.com/memberlist.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

if(!$page = curl_exec($ch))
{
    echo "page fail";
}

echo $page;

curl_close($ch);

I get a "page fail" being printed to the page, so I guess the logging in isn't working.

Any help? thanks.


Nevermind, downloaded a phpBB cURL library and sorted it.


Add the CURLOPT_COOKIEFILE option with same value as the CURLOPT_COOKIEJAR option.

You also need to use those two options for each subsequent request (ie your request for memberlist.php).

You can see a way of doing it here.

0

精彩评论

暂无评论...
验证码 换一张
取 消