php 使用 curl 提交/检索表单结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4589160/
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 13:36:07  来源:igfitidea点击:

Using curl to submit/retrieve a forms results

phppostcurllibcurl

提问by Jason

I need help in trying to use curl to post data to a page and retrieve the results after the form has been submitted.

我需要帮助尝试使用 curl 将数据发布到页面并在提交表单后检索结果。

I created a simple form:

我创建了一个简单的表单:

<form name="test" method="post" action="form.php">              
    <input type="text" name="name" size="40" />
    <input type="text" name="comment" size="40" />
    <input type="submit" value="submit" />
</form>

In addition, I have php code to handle this form in the same page. All it does is echo back the form values.

此外,我有 php 代码可以在同一页面中处理此表单。它所做的只是回显表单值。

The curl that I have been using is this:

我一直在使用的卷曲是这样的:

  $h = curl_init();

  curl_setopt($h, CURLOPT_URL, "path/to/form.php"); 
  curl_setopt($h, CURLOPT_POST, true);
  curl_setopt($h, CURLOPT_POSTFIELDS, array(
  'name' => 'yes',
  'comment' => 'no'
  ));
  curl_setopt($h, CURLOPT_HEADER, false);
  curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);

  $result = curl_exec($h);
  echo $result;

When I launch the page with the curl code in it, I get the form.php page contents but it doesn't not show the variables that PHP should have echo'd when the form is submitted.

当我启动带有 curl 代码的页面时,我得到了 form.php 页面内容,但它没有显示提交表单时 PHP 应该回显的变量。

would appreciate any help with this.

将不胜感激任何帮助。

Thanks.

谢谢。

采纳答案by dqhendricks

AHHHH after your comment, the problem is clear.

AHHHH 在您发表评论后,问题就清楚了。

you need to include one more POST variable in your array.

您需要在数组中再包含一个 POST 变量。

'submitted' => 'submitted'

the submit button also returns a post value if clicked, which you are checking in your form processing PHP.

如果单击,提交按钮也会返回一个 post 值,您正在处理 PHP 的表单中检查该值。

if ($_POST['submitted']) {

in your curl code however you have left out the 'submitted' post variable.

但是,在您的 curl 代码中,您遗漏了“提交”后变量。

if this is what you are looking for, please select the check mark next to this answer. thanks!

如果这是您要查找的内容,请选中此答案旁边的复选标记。谢谢!

回答by Luke Stevenson

From reading the other two answers, and the comment by the OP, I have a couple of ideas.

通过阅读其他两个答案以及 OP 的评论,我有一些想法。

Specifically, the OP Comment was:

具体来说,OP评论是:

Submitting the form manually does produce the right output. The PHP that handles the form is: if(isset($_POST['submitted'])){echo $_POST[name] ..... etc. and that's it

手动提交表单确实会产生正确的输出。处理表单的 PHP 是: if(isset($_POST['submitted'])){echo $_POST[name] ..... 等等,就是这样

Under standard conditions, your basic form, as noted in the Original Question, would generate a $_POST array like the following:

在标准条件下,如原始问题中所述,您的基本形式将生成一个 $_POST 数组,如下所示:

array(
  'name' => 'The Name as Entered in the Form' ,
  'comment' => 'The Comment as Entered in the Form' ,
  'submit' => 'submit' # From the "Submit" button
);

Your comment suggests that some aspect of your form handler is looking for a $_POST element called "submitted".

您的评论表明您的表单处理程序的某些方面正在寻找一个名为“已提交”的 $_POST 元素。

1) The basic form, as set out in the question, will always return FALSE for a check for $_POST['submitted'], and, as such, will trigger the ELSE action (if present) for that conditional.

1) 如问题中所述,基本形式将始终返回 FALSE 以检查 $_POST['submitted'],因此,将触发该条件的 ELSE 操作(如果存在)。

2) Your CURL Action does not set either 'submit' or 'submitted', and, again, will always return FALSE for the conditional.

2) 您的 CURL 操作未设置“提交”或“已提交”,并且再次为条件返回 FALSE。

So, I would suggest that you:

所以,我建议你:

1) Check your Form Handler, and see what fields are essential, what their names are and what their content should be.

1) 检查您的表单处理程序,看看哪些字段是必不可少的,它们的名称是什么以及它们的内容应该是什么。

2) Check your Basic Form, and ensure that it has the appropriate fields.

2) 检查您的基本表格,并确保其具有适当的字段。

3) Check your CURL Action, and ensure that it details every field which is required. (Easy check would be to insert print_r( $_POST );die();at the top of your Form Handler and submit the basic form. This will show you exactly what the form is sending, so you can recreate it in CURL.

3) 检查您的 CURL 操作,并确保它详细说明了每个必填字段。(简单的检查方法是print_r( $_POST );die();在表单处理程序的顶部插入并提交基本表单。这将准确显示表单发送的内容,因此您可以在 CURL 中重新创建它。