Php 如何使用curl自动填充和提交html表单

Php 如何使用curl自动填充和提交html表单,php,forms,curl,Php,Forms,Curl,我想自动填写html表单,提交表单并显示结果。我用下面的代码得到的 我使用Facebook移动网站和gmail来测试这段代码 我将这些站点的登录页面的url放在curl_init函数中,将登录页面的用户名和密码字段的name属性值放入$post_数据数组的键中,并将此代码保存为my.php文件,并将其放在本地机器的xampp htdocs目录中 当我浏览my.php时,它会显示登录页面,其中用户名字段已填充,密码字段未填充。根据代码,预期结果是,它应该返回成功登录的页面,因为我提供了正确的用

我想自动填写html表单,提交表单并显示结果。我用下面的代码得到的


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

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

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

在查看代码时,我发现有一些隐藏字段,您可以(应该)尝试发送。通常这些都是为了防止自动投递


首先,使用一些DOM解析器获取隐藏字段,并构建查询以将其发布到操作url。

谢谢您的回答。您可以通过显示如何使用这些隐藏字段构建查询来扩展答案吗?在上面的代码中,我使用了post_数据数组来保留username和password字段的值。但这些隐藏字段已经有了值。生成查询时应使用哪些值?我看到自动完成是从那些隐藏的领域。那么我可以用查询填写它们吗?
<?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);
?>