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

问题描述:

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

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

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

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>

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

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

我一直使用的 curl 是这样的:

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;

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

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.

非常感谢您的帮助.

谢谢.

AHHHH 看了你的评论,问题就清楚了.

AHHHH after your comment, the problem is clear.

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

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

'submitted' => 'submitted'

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

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

if ($_POST['submitted']) {

在你的 curl 代码中,你已经遗漏了提交"的帖子变量.

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

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

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