How would I go about writing a simple PHP bot?

2020-03-31 08:53发布

How would I go about writing a simple PHP bot that could login in and recieve all the cookies sent by the server? ... and then send them back when required?

Any suggestions would be appreciated.

标签: php cookies bots
3条回答
对你真心纯属浪费
2楼-- · 2020-03-31 09:07

With the help of the cURL library?

查看更多
Animai°情兽
3楼-- · 2020-03-31 09:09

First of all, your question is too broad and lacking in detail to really answer effectively. That said, I'll give it a try.

Not knowing what exactly you mean by "log in", I assume you want the script to be able to post some data to another script via an HTTP request. The CURL Library is good for that. It is able to post data and handle cookies.

Edit: Got ninja'd by Zed. ;)

查看更多
Rolldiameter
4楼-- · 2020-03-31 09:15

If for some reason you cannot use the curl extension on your server (extension not installed), you can use a class such as Snoopy which will still allow you to either use the curl binaries or use sockets to retrieve the information.

Snoopy handles cookies.

As for the writing the bot itself, it's just a question of sending the proper requests. Here is an example with Snoopy:

$snoopy = new Snoopy;

// The following needs to reflect the form configuration of the site
$login = array('usr' => 'hi', 'pwd' => 'hello');

if($snoopy->submit('http://example.com/login', $login) === false) {
    // output the response code
    die($snoopy->response_code . ':' . $snoopy->error);
}

//Request succeeded (doesn't mean we are logged in)
// output the results
echo $snoopy->results;

// Check the results to see if you are logged in and
// Continue using $snoopy.
// It will pass the proper cookies for the next requests.
查看更多
登录 后发表回答