PHP Bot Instance Code _php tutorial

Source: Internet
Author: User

PHP Bot Instance Code


Online about the PHP bot program is still very few instances, the previous period of time is also a business need to begin to contact such programs, very interesting. The so-called bot is actually analog get or post, go to action some programs, to achieve some automated processing, of course, this thing is a double-edged sword, can not be bad.

PHP implementation of the bot there are many ways, the individual prefer HttpRequest, one comparison Oo, and to write simple and convenient. Here is the function for class and some examples.

function can be directly clicked into the official PHP API, interested friends in the Tour; the 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 execute send, send this GET request when Getresponsecode is 200, that is, when the bot succeeds, The response HTML returned by the GET request is stored in 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 ' = ') '));

$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 simulates post to request a PHP file, post does not pass addquerydata such function, but through addpostfields to set the analog input form, and then execute send, Returns the response HTML echo of the POST request to the current PHP page.

http://www.bkjia.com/PHPjc/847204.html www.bkjia.com true http://www.bkjia.com/PHPjc/847204.html techarticle PHP Bot Instance code on the Internet on the PHP bot instance is still very few, the previous period of time is also a business need to begin to contact such programs, very interesting. The so-called bot is actually analog get or ...

  • Related Article

    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.