使用curl提交/检索表单结果 [英] Using curl to submit/retrieve a forms results

查看:112
本文介绍了使用curl提交/检索表单结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我创建了一个简单的表单: / p>

 < form name =testmethod =postaction =form.php> 
< input type =textname =namesize =40/>
< input type =textname =commentsize =40/>
< input type =submitvalue =submit/>
< / form>

此外,我有PHP代码在同一页面处理此表单。



我一直在使用的卷曲是:

  $ 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在表单提交时应该echo'd的变量。



会感谢任何帮助。



谢谢。

解决方案

AHHHH在您的评论后,问题很清楚。



您需要在数组中添加一个POST变量。

 'submitted'=> 'submit'

如果单击,提交按钮也会返回一个post值处理PHP。

  if($ _POST ['submitted']){
/ pre>

在您的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>

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.

解决方案

AHHHH after your comment, the problem is clear.

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

'submitted' => 'submitted'

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

if ($_POST['submitted']) {

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!

这篇关于使用curl提交/检索表单结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆