search
Homephp教程php手册PHP BOT实例代码

PHP BOT实例代码

Jun 13, 2016 am 09:28 AM
phpbusinesscodeaboutExampleofprogramstillneed

PHP BOT实例代码

   网上关于PHP BOT程序的实例还是很少的,前段时间也是业务需求,开始接触此类程序,很有意思。所谓BOT实际上是模拟GET或POST,去action一些程序,实现一些自动化处理,当然这个东西是双刃剑,可别使坏就好。

  PHP实现BOT有多种方式,个人比较喜欢HttpRequest,一来比较OO,而来编写简单方便。以下是class对应的function,还有一些examples。

  function可以直接点击进入PHP官方的API,有意的朋友进去逛逛;方法名称很直观,不多解释。。 Example #1 GET example

  代码

  $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;

  }

  ?>

  这个example模拟get去请求一个rss订阅器,还addQueryData这样的GET查询参数,然后执行send,发送此GET请求当getResponseCode是200时,也就是BOT成功时,把get请求返回的response的HTML存入本地的文件中。

  Example #2 POST example

  代码

  $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;

  }

  ?>

  这个example模拟POST去请求一个PHP文件,POST不是通过addQueryData这样的function,而是通过addPostFields来设置模拟的输入表单,然后执行send,把POSt请求返回的response的html echo到PHP当前页面中。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor