Using curl to submit/retrieve a forms results

2019-01-17 08:36发布

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>

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

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.

would appreciate any help with this.

Thanks.

0条回答
登录 后发表回答