【问题标题】:How to pass the result of a form to another website with cURL?如何使用 cURL 将表单的结果传递到另一个网站?
【发布时间】:2013-12-05 13:18:26
【问题描述】:

我是一名刚接触网络开发世界的学生。请原谅我有一些错误和知识不足。

目前正在研究PHP cUrl,因为根据我的朋友和我的研究,它是否适合我当前的项目仍然不确定。

我正在创建一个表单,如果用户输入一些内容然后单击提交按钮,那么表单中的结果将被传递到另一个网站,然后我将输出该结果。

从来没有使用 cUrl 的背景,最近才了解它的基本结构。 所以我发布这篇文章是为了获得一些关于如何将它用于我的项目的建议。感谢所有帮助 tnx。

我目前学到的基本结构如下:

$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'http://somesite.com');
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

if (empty($buffer))
{
    print "Yeah.<p>";
}
else
{
    print $buffer;
}
?>

【问题讨论】:

    标签: php html curl


    【解决方案1】:

    你需要利用这些cURL参数

    curl_setopt($curl_handle,CURLOPT_POST,1); //You have to enable the POST 
    curl_setopt($curl_handle,CURLOPT_POSTFIELDS,$yourformfields); //Your form fields will be sent here
    

    重写你的例子...

    $username=$_POST['username'];//values from your form
    $password=$_POST['password'];
    $yourformfields="username=$username&password=$password";
    $curl_handle=curl_init();
    curl_setopt($curl_handle,CURLOPT_URL,'http://somesite.com');
    curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
    curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($curl_handle,CURLOPT_POST,1);
    curl_setopt($curl_handle,CURLOPT_POSTFIELDS,$yourformfields);
    $buffer = curl_exec($curl_handle);
    curl_close($curl_handle);
    
    if (empty($buffer))
    {
        print "Yeah.<p>";
    }
    else
    {
        print $buffer;
    }
    ?>
    

    【讨论】:

      【解决方案2】:

      您也可以通过直接将表单操作设置为其他网站上的 url 来尝试仅使用 HTML 的解决方案。在这种情况下不需要 cURL 或 PHP。如果 method="post" 不起作用,您可能还想尝试 method="get"。

      <form action="http://otherwebsite.com/formhandle.php" method="post">
        ...
      </form>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-21
        • 1970-01-01
        • 1970-01-01
        • 2016-01-06
        • 2019-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多