我如何去编写一个简单的PHP bot? [英] How would I go about writing a simple PHP bot?

查看:329
本文介绍了我如何去编写一个简单的PHP bot?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何编写一个简单的PHP机器人,可以登录并接收服务器发送的所有Cookie? ...

解决方案

如果由于某种原因不能在服务器上使用 curl 扩展(扩展未安装),则可以使用 Snoopy ,仍然允许您使用 curl 二进制文件或使用套接字检索信息。



Snoopy处理cookie。



至于编写bot本身,它只是一个发送正确请求的问题。下面是Snoopy的示例:

  $ snoopy = new Snoopy; 

//以下需要反映网站的表单配置
$ login = array('usr'=>'hi','pwd'=>'hello') ;

if($ snoopy-> submit('http://example.com/login',$ login)=== false){
//输出响应代码
die($ snoopy-> response_code。':'。$ snoopy-> error);
}

//请求成功(不意味着我们已经登录)
//输出结果
echo $ snoopy-> results;

//检查结果以查看您是否已登录,以及
//继续使用$ snoopy。
//它将为下一个请求传递正确的cookie。


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.

解决方案

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.

这篇关于我如何去编写一个简单的PHP bot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆