选择使用cURL填写相同输入名称的表格

时间:2018-08-18 05:16:50

标签: php html forms curl

我有这个HTML / PHP代码,它有两种形式:一种用于登录,一种用于登录。 我想用php cURL填充它们。到目前为止,这是我的代码:

我的form.php文件代码如下

<html>  
<head>  
</head> 
<body>
    <form id="login" action="login.php" method="post">
        Name: <input type="text" name="name"><br>
        E-mail: <input type="text" name="email"><br>
        <input type="submit">
    </form>
    <form id="register" action="register.php" method="post">
        Name: <input type="text" name="name"><br>
        E-mail: <input type="text" name="email"><br>
        <input type="submit">
    </form>     
</body> 
</html>

<?php 
$ch = curl_init("form.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);

$data = array(
'name' => 'test',
'email' => 'test'
);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);    
?>

我的问题是:由于输入名称相同,如何选择要填写的表格?

我知道我可以更改输入的名称,但是我对可能存在的答案很感兴趣。

谢谢(PS:我是法语,所以我的英语可能不是很完美):)

2 个答案:

答案 0 :(得分:1)

我认为cUrl仅对进行http / https调用有用。

您需要的是无头浏览器,请尝试phantom JS,我在Node Projects中使用了该浏览器,它们也有一个PHP版本:http://jonnnnyw.github.io/php-phantomjs/

您将javascript调用发送到PhantomJS,您的javascript函数返回的任何数据都将返回给您以供您访问-在您的情况下为php。

答案 1 :(得分:0)

经过一番搜索,我找到了想要的东西。我误解了cURL的目的。我以为它会像浏览器用户一样发送表单,但是它只是将POST / PUT请求发送到网页。 确实PhantomJS可以工作,但我更喜欢Python方式。 我将尝试使用我也很熟悉的Python进行机械化。 谢谢您的回答:)