如何提交从 URL 检索表单并使用 PHP 提交?

我找到了一个任务来访问我的技能。任务是从给定的 URL 获取表单并填写表单。该表单将有 3 个输入,如姓名、电子邮件、计数和正方形,即“x”和“ ”的组合。填写所有字段,在名为 的输入框中count,我必须填写“ ”的计数,然后我必须提交表单。我必须只使用 PHP。


表单应提交到我获取表单的同一 URL。我曾尝试使用 cURL,但它再次返回表单而不是预期的输出。


if(!empty($_POST)){

    echo "<pre>";

    print_r($_POST);

    echo "</pre>";

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_POST, true);

    $data = $_POST;

    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    $out = curl_exec($ch);

    $inf = curl_getinfo($ch);

    curl_close($ch);


    echo "<pre>";

    print_r($out);

    print_r($inf);

    echo "</pre>";

    exit;

  }

提交表单后,我希望得到“感谢提交”。文本。相反,我得到与输出相同的形式。


慕容708150
浏览 145回答 2
2回答

慕哥6287543

也许问题是您在数组中发送 post 字段值而不是 URL 编码,使用http_build_query或 json_encode 并检查。检查下面的代码。if(!empty($_POST)){&nbsp; &nbsp; &nbsp; &nbsp; echo "<pre>";&nbsp; &nbsp; &nbsp; &nbsp; print_r($_POST);&nbsp; &nbsp; &nbsp; &nbsp; echo "</pre>";&nbsp; &nbsp; &nbsp; &nbsp; $ch = curl_init();&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt($ch, CURLOPT_URL, $url);&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt($ch, CURLOPT_POST, true);&nbsp; &nbsp; &nbsp; &nbsp; $data = $_POST;&nbsp; &nbsp; &nbsp; &nbsp; /*** you are direct sending the array value&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; use http_build_query&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;or&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$data_string = json_encode($data);&nbsp; &nbsp; &nbsp; &nbsp; ***/&nbsp; &nbsp; &nbsp; &nbsp; $data_string = http_build_query($data);&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string );&nbsp; &nbsp; &nbsp; &nbsp; $out = curl_exec($ch);&nbsp; &nbsp; &nbsp; &nbsp; $inf = curl_getinfo($ch);&nbsp; &nbsp; &nbsp; &nbsp; curl_close($ch);&nbsp; &nbsp; &nbsp; &nbsp; echo "<pre>";&nbsp; &nbsp; &nbsp; &nbsp; print_r($out);&nbsp; &nbsp; &nbsp; &nbsp; print_r($inf);&nbsp; &nbsp; &nbsp; &nbsp; echo "</pre>";&nbsp; &nbsp; &nbsp; &nbsp; exit;&nbsp; &nbsp; &nbsp; }

冉冉说

PHP 不能对表单做任何事情。在这种情况下,您应该获取 html 表单 (curl是一个选项。然后解析表单以获取表单方法 (get/post)、它的操作 (url) 和所有输入名称。为此,您可以使用类似xpath 的东西从那里,您通过根据上述给定信息制作数据来提交另一个请求。
打开App,查看更多内容
随时随地看视频慕课网APP