Home  >  Article  >  Backend Development  >  PHP BOT example code_PHP tutorial

PHP BOT example code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:22:40933browse

PHP BOT example code

There are still very few examples of PHP BOT programs on the Internet. It was also a business requirement some time ago. It is very interesting to start contacting such programs. The so-called BOT actually simulates GET or POST to act on some programs and realize some automated processing. Of course, this thing is a double-edged sword, so don't make it bad.

There are many ways to implement BOT in PHP. I personally like HttpRequest because it is more OO and easy to write. The following are the functions corresponding to the class, as well as some examples.

Function can be directly clicked to enter the official PHP API. Friends who are interested can go in and have a look; the method names are very intuitive and do not require much explanation. . Example #1 GET example

Code

$r = new HttpRequest('http://example.com/feed.rss', HttpRequest::METH_GET);

 $r->setOptions(array('lastmodified' => filemtime('local.rss')));

 $r->addQueryData(array('category' => 3));

try {

 $r->send();

 if ($r->getResponseCode() == 200) {

file_put_contents('local.rss', $r->getResponseBody());

 }

 } catch (HttpException $ex) {

echo $ex;

 }

 ?>

This example simulates get to request an rss subscriber, and also adds GET query parameters such as addQueryData, and then executes send to send this GET request. When the getResponseCode is 200, that is, when the BOT is successful, the response HTML returned by the get request is Save to a local file.

Example #2 POST example

Code

$r = new HttpRequest('http://example.com/form.php', HttpRequest::METH_POST);

 $r->setOptions(array('cookies' => array('lang' => 'de')));

 $r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));

$r->addPostFile('image', 'profile.jpg', 'image/jpeg');

try {

echo $r->send()->getBody();

 } catch (HttpException $ex) {

echo $ex;

 }

 ?>

This example simulates POST to request a PHP file. POST does not use functions such as addQueryData, but sets the simulated input form through addPostFields, and then executes send to echo the html of the response returned by the POSt request to the current PHP page. .

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/847204.htmlTechArticlePHP BOT Example Code There are still very few examples of PHP BOT program on the Internet. It was also a business requirement some time ago, so I started to contact This type of program is very interesting. The so-called BOT actually simulates GET or...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn