如何使用cURL将表单的结果传递给另一个网站?

时间:2013-12-05 13:18:27

标签: php html curl

我是一名不熟悉网络发展中国家的学生。如果我有一些错误和知识不足,请与我同行。

目前正在研究PHP cUrl,因为根据我的朋友和我的研究,我的当前项目中的php库仍然不确定。

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

从未有过使用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;
}
?>

2 个答案:

答案 0 :(得分:0)

您需要使用这些 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;
}
?>

答案 1 :(得分:0)

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

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