简体   繁体   English

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

[英]How to pass the result of a form to another website with cURL?

I am a student who is new to the web developing world.Please bare w/ me for having some mistakes and insufficient knowledge. 我是网络开发领域的新手。请告诉我一些错误和知识不足。

Currently studying about the PHP cUrl, because according to my friends and base on my research, it is the php library that would fit in my current project still not sure though. 当前正在研究PHP cUrl,因为根据我的朋友们以及根据我的研究,仍然不确定是否适合我当前项目的php库。

I am creating a form, were a user inputs something then clicks the submit button, the result in the form then will be pass to another website and then i will output that result. 我正在创建一个表单,如果用户输入了一些内容,然后单击“提交”按钮,则表单中的结果将被传递到另一个网站,然后我将输出该结果。

Never had a background in using cUrl and just recently learned its basic structure. 从来没有使用cUrl的背景,而只是最近才了解其基本结构。 So I'm posting this to get some suggestions on how I can use it for my project. 因此,我发布此消息是为了获得一些有关如何在项目中使用它的建议。 Appreciate all the help tnx. 感谢所有帮助tnx。

Basic Structure that I currently learned below: 我目前在下面学习的基本结构:

$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;
}
?>

You need to make use of these cURL parameters 您需要使用这些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

Rewriting your example... 重写您的示例...

$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;
}
?>

You could also just try an HTML-only solution by directly setting the form action to the url on the other website. 您也可以通过直接将表单操作设置为另一个网站上的url来尝试仅HTML的解决方案。 No need for cURL or PHP in that case. 在这种情况下,不需要cURL或PHP。 You might also want to try method="get" if method="post" does not work. 如果method =“ post”不起作用,您可能还想尝试method =“ get”。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM