PHP Bot Instance Code

Source: Internet
Author: User
Tags php file

There are few examples of PHP bot programs on the Web, which is also a business requirement, and it is very interesting to start contacting such programs. The so-called bot is actually simulate get or post, to action some programs, to achieve some automated processing, of course, this thing is a double-edged sword, but do not be bad.

PHP implementation of the bot there are a variety of ways, individuals prefer httprequest, one more oo, and to write simple and convenient. Here is the class corresponding function, and there are some examples.

function can be directly clicked into the official PHP API, interested friends to go to visit; Method name is very intuitive, not 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, also addquerydata such a get query parameter, and then executes send, sending the GET request when the Getresponsecode is 200, which is when the bot succeeds, Stores the response HTML returned by a GET request into a local file.

Example #2 POST Example

Code

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

$r->setoptions (Array (' Cookie ' => array (' lang ' => ' de '));

$r->addpostfields (' user ' => ' Mike ', ' Pass ' => ' s3c|r3t '));

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

try {

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

catch (HttpException $ex) {

Echo $ex;

}

?>

This example simulation post to request a PHP file, post not by addquerydata such function, but through the addpostfields to set up the analog input form, and then execute send, Returns the response HTML echo from the POST request to the current page in PHP.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.