php 创建一个“机器人”来填写表单中的一些页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17270335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:35:47  来源:igfitidea点击:

Creating a 'robot' to fill form with some pages in

php.netweb-servicesbots

提问by Dima

I want to implement an 'robot' that could automatically fill forms. Is there an solution when you can fill data on page for example,form1.htmland submit it, wait to next page and submit with data on form2.html,and so on... In the end it should also 'click' on a button to get a file that the form creates.

我想实现一个可以自动填写表格的“机器人”。例如,当您可以在页面上填写数据form1.html并提交时,是否有解决方案,等待下一页并提交数据form2.html,等等......最后它也应该“单击”按钮以获取文件表单创建的。

I want this 'robot' would use some confidential information, so it cant be done using client sidetechnologies.

我希望这个“机器人”会使用一些机密信息,因此无法使用client side技术来完成。

I was thinking about PHP- building it as a web site-web service, so you could transfer data to a web address, or a Web Service in .Net.

我在考虑PHP- 将它构建为一个网站 - 网络服务,这样你就可以将数据传输到一个网址,或者.Net.

If it's important,the site I want to fill automatically is runs with ASP.NET.

如果这很重要,我想自动填充的站点将使用ASP.NET.

I kind a new here...Can anyone give some examples or tutorials doing this thing. If exist some technologies that I didn't mention here to realize it I would be glad trying them also.

我在这里是一个新人......任何人都可以举出一些例子或教程来做这件事。如果存在一些我没有在这里提到的技术来实现它,我也很乐意尝试它们。

回答by Adam

Forms work by posting data, so instead of making a robot that would type something into every field and click submit, you can just POSTthe data to the server.

表单通过发布数据来工作,因此无需让机器人在每个字段中输入内容并单击提交,您只需POST将数据发送到服务器即可。

First grab the form fields names, and the actionof the form.

首先获取表单字段名称和表单的名称action

Then CURL:

然后卷曲:

//set POST variables
$url = 'http://domain.com/get-post.php';
$fields = array(
                        'lname' => urlencode($last_name),
                        'fname' => urlencode($first_name),
                        'title' => urlencode($title),
                        'company' => urlencode($institution),
                        'age' => urlencode($age),
                        'email' => urlencode($email),
                        'phone' => urlencode($phone)
                );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

Snippet from this site.

来自本网站的片段。

回答by Voice

Use Selenium.

使用

"Selenium automates browsers. That's it. What you do with that power is entirely up to you. Primarily it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) also be automated as well."

“Selenium 使浏览器自动化。就是这样。你用这种能力做什么完全取决于你。主要是为了自动化 Web 应用程序以进行测试,但肯定不仅限于此。无聊的基于 Web 的管理任务可以(并且应该) !) 也可以自动化。”

See examples here.

请参阅此处的示例。

enter image description here

在此处输入图片说明