【问题标题】:how to auto fill and submit html form with curl如何使用 curl 自动填写和提交 html 表单
【发布时间】:2013-05-18 17:07:48
【问题描述】:

我想自动填写一个 html 表单并提交表单并显示结果。我使用了以下来自here的代码。

<?php
//create array of data to be posted
$post_data['email'] = 'myemail';
$post_data['pass'] = 'mypassword';

//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//create cURL connection
$curl_connection = 
  curl_init('http://m.facebook.com/');

//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, 
  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);

print $result;
//show information regarding the request
echo curl_errno($curl_connection) . '-' . 
                curl_error($curl_connection);

//close the connection
curl_close($curl_connection);
?>

我使用 Facebook 移动网站和 gmail 来测试这段代码。

我将这些站点的登录页面的url放在curl_init函数中,将登录页面的用户名和密码字段的名称属性的值赋给$post_data数组的键,并将这段代码保存为my.php文件并放置在本地机器中的 xampp htdocs 目录。

当我浏览 my.php 时,它显示登录页面,其中用户名字段已填写,密码字段未填写。根据代码,预期的结果是,它应该返回成功登录的页面,因为我提供了正确的用户名和密码。 curl_errno 也返回 0。这意味着没有发生错误。那为什么我不能得到预期的结果?以及为什么填写了用户名字段,但没有填写密码字段?

【问题讨论】:

    标签: php forms curl


    【解决方案1】:

    检查http://m.facebook.com/ 的代码,我看到有一些隐藏字段,您可能(应该)尝试发送。通常这些是为了防止自动 POST。

    首先获取 http://m.facebook.com/ 并使用一些 DOM 解析器获取隐藏字段并构建查询以将它们发布到操作 url。

    【讨论】:

    • 感谢您的回答。您能否通过展示如何使用这些隐藏字段构建查询来扩展答案。在上面的代码中,我使用 post_data 数组来保留用户名和密码字段的值。但是那些隐藏字段已经有值了。构建查询时应该使用哪些值?我看到自动完成功能不在那些隐藏字段中。那么我可以用查询填充它们吗?
    猜你喜欢
    • 2012-06-19
    • 2021-11-12
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 1970-01-01
    • 2011-05-16
    相关资源
    最近更新 更多