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 ...