当前位置:首页 > PHP

php实现自动填表

2026-01-30 08:19:35PHP

PHP实现自动填表的方法

使用PHP实现自动填表可以通过多种方式完成,主要取决于目标表单的类型和提交方式。以下是几种常见的方法:

cURL库提交表单数据

PHP的cURL库可以模拟浏览器提交表单数据。需要构建POST或GET请求,并设置必要的头部信息。

$url = 'https://example.com/form-submit';
$data = [
    'username' => 'testuser',
    'password' => 'testpass',
    'submit' => 'Submit'
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

file_get_contents提交表单

对于简单的表单提交,可以使用file_get_contents结合stream_context_create。

$url = 'https://example.com/form-submit';
$data = [
    'username' => 'testuser',
    'password' => 'testpass'
];

$options = [
    'http' => [
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data)
    ]
];

$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);

使用Guzzle HTTP客户端

php实现自动填表

Guzzle是一个流行的PHP HTTP客户端,可以更简洁地处理表单提交。

require 'vendor/autoload.php';

$client = new \GuzzleHttp\Client();
$response = $client->post('https://example.com/form-submit', [
    'form_params' => [
        'username' => 'testuser',
        'password' => 'testpass'
    ]
]);

$body = $response->getBody();

处理JavaScript渲染的表单

对于需要执行JavaScript的表单,可以使用PHP结合无头浏览器如Puppeteer。

php实现自动填表

// 需要安装chrome-php/chrome
use HeadlessChromium\BrowserFactory;

$browserFactory = new BrowserFactory();
$browser = $browserFactory->createBrowser();

$page = $browser->createPage();
$page->navigate('https://example.com/form')->waitForNavigation();

$page->evaluate('document.querySelector("#username").value = "testuser"');
$page->evaluate('document.querySelector("#password").value = "testpass"');
$page->evaluate('document.querySelector("form").submit()');

$browser->close();

处理CSRF令牌

许多表单包含CSRF保护,需要先获取令牌再提交。

// 首先获取包含CSRF令牌的页面
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com/form');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($ch);

// 解析HTML获取CSRF令牌
preg_match('/name="csrf_token" value="(.*?)"/', $html, $matches);
$csrf_token = $matches[1];

// 使用令牌提交表单
$data = [
    'username' => 'testuser',
    'password' => 'testpass',
    'csrf_token' => $csrf_token
];
curl_setopt($ch, CURLOPT_URL, 'https://example.com/form-submit');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$response = curl_exec($ch);
curl_close($ch);

处理验证码

对于有验证码的表单,可以集成第三方验证码识别服务或使用OCR技术。

// 获取验证码图片
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com/captcha.jpg');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$captcha_image = curl_exec($ch);

// 保存图片并发送到验证码识别API
file_put_contents('captcha.jpg', $captcha_image);
$captcha_text = file_get_contents('http://captcha-api.com/recognize?image=captcha.jpg');

// 提交带验证码的表单
$data = [
    'username' => 'testuser',
    'password' => 'testpass',
    'captcha' => $captcha_text
];
curl_setopt($ch, CURLOPT_URL, 'https://example.com/form-submit');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$response = curl_exec($ch);
curl_close($ch);

注意事项

自动填表可能违反某些网站的服务条款,使用前应确保获得授权。处理敏感数据时要特别注意安全性,避免在代码中硬编码凭证。对于复杂的表单交互,可能需要模拟完整的用户会话,包括cookie管理。

标签: php
分享给朋友:

相关文章

php实现文件上传

php实现文件上传

PHP 文件上传实现方法 在 PHP 中实现文件上传需要处理 HTML 表单和服务器端脚本。以下是详细步骤: 创建 HTML 表单 使用 enctype="multipart/form-data"…

php实现注册

php实现注册

注册功能实现步骤 数据库准备 创建用户表,包含字段如id、username、password、email等。示例SQL: CREATE TABLE users ( id INT AUTO_I…

php实现推送

php实现推送

PHP 实现推送功能的方法 PHP 实现推送功能可以通过多种方式完成,包括 WebSocket、Server-Sent Events (SSE)、长轮询或第三方推送服务。以下是几种常见的方法: 使用…

php 实现秒杀

php 实现秒杀

PHP 实现秒杀的关键技术 秒杀系统需要解决高并发、超卖、性能瓶颈等问题。以下是实现秒杀的核心方案: 使用 Redis 预减库存 将商品库存提前加载到 Redis 中,通过原子操作保证库存准确性:…

php 实现单链表

php 实现单链表

单链表的基本概念 单链表是一种线性数据结构,由节点组成,每个节点包含数据域和指向下一个节点的指针域。链表的头节点是访问整个链表的入口。 单链表的节点类实现 在PHP中,可以通过类来定义链表节点。每个…

php实现定时

php实现定时

PHP 实现定时任务的方法 PHP 本身是脚本语言,通常需要借助外部工具或框架实现定时任务。以下是几种常见方法: 使用 Cron 任务 Linux 系统自带的 Cron 是常用的定时任务工具。通过编…