使用curl和php自动填写表格 [英] Fill out a form automatically using curl and php

查看:202
本文介绍了使用curl和php自动填写表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本,该脚本会自动填写表格,然后自动按下提交"按钮.

I am trying to write a script that fills out forms automatically and then automatically presses the submit button.

我已经读到您可以使用curl来发布HTTP请求,但是当表单使用JavaScript处理发布请求时(如下面的代码),您会怎么做?

I have read that you can use curl to post HTTP requests, but what do you do when the form handles the post request with JavaScript, like the code below?

<form name="myform" action="javascript:void(0)" method="POST" onsubmit="return fancy_function(this)">

推荐答案

基于对以上评论的回答,您要做的不是用curl来填写表单",而是将填写好的表单发布到表单上.表单通常会发送到的同一页面.

Based off your answer to the comments above, what you want to do is not "fill out the form" with curl, but rather post the completed form to the same page the form would normally send to.

这也称为跨站点发布,许多开发人员专门测试并且不允许这样做以提高安全性.如果表单有验证码,这也将变得更加困难.

This is also called cross-site posting, and many developers specifically test for and don't allow it to improve security. This will also be much much harder if the form has a captcha.

假设这样做仍然有意义(我以前在通讯注册表单中使用过此技术),这就是您想要做的事情:

Assuming that it still makes sense to do (I've used this technique before for a newsletter signup form), here's what you want to do:

要获取表单的操作网址,您需要浏览该站点的Javascript代码,并找到在其中定义funcy_function()的位置.然后,在函数主体中,您很可能会看到目标网址.您还需要注意html中表单变量的特定名称.您需要使用完全相同的名称设置变量.然后您的curl设置将如下所示:

To get the form's action url, you'll need to look through the Javascript code for the site and find where funcy_function() is defined. Then in the body of the function, you'll likely see the destination url. You'll also need to note the specific names of the form variables in the html. You'll need to setup your variables with the exact same names. Then your curl setup will look like this:

$url = 'http://www.targeturl.com';
$myvars = 'myvar1=' . $myvar1 . '&myvar2=' . $myvar2;

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec( $ch );

这会将post变量发送到指定的url,从而模仿了表单中通常会发生的情况.如果您要解析或使用该页面,则该页面返回的html将在$ response中.

This will send the post variables to the specified url, imitating what would normally happen from the form. The html that the page returns will be in $response if you want to parse or use it.

这篇关于使用curl和php自动填写表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆