如何使用 PHP 和 cURL 提交 POST 数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6968857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I submit POST data using PHP and cURL?
提问by Ole Media
Can anyone help on how can I do to send the data from a form (POST) to an URL using PHP and cURL?
任何人都可以帮助我如何使用 PHP 和 cURL 将数据从表单 (POST) 发送到 URL?
Using cURL, all I want to accomplish is send the data, I don't want to be redirected nor get any sort of output (HTML nor TEXT) just submit the data.
使用 cURL,我想要完成的只是发送数据,我不想被重定向或获得任何类型的输出(HTML 或 TEXT),只需提交数据即可。
It would be nice to know if the data has been submitted successfully or not to handle error or redirection.
很高兴知道数据是否已成功提交以处理错误或重定向。
Additional Info. The reason I think I'm getting redirected is because once I execute the cURL, the destination page, which is a third party website, have a redirect into place confirming the user that their data has been received, and for some reason, when I send my data using cURL their redirection it effects my page, therefore my page it also redirects to their confirmation site.
附加信息。我认为我被重定向的原因是因为一旦我执行 cURL,目标页面(第三方网站)就会重定向到位,确认用户已收到他们的数据,并且出于某种原因,当我使用 cURL 发送我的数据,它们的重定向会影响我的页面,因此我的页面也会重定向到他们的确认站点。
Thank you all.
谢谢你们。
Sample Code:
示例代码:
$sub_req_url ="http://domain.com/util/request";
$ch = curl_init($sub_req_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, "id=64&session=1&k=B0EA8835E&firstname=Name&lastname=Lastname&company=Company%20Name&[email protected]&work_phone=123-456-7890");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
$resp = curl_exec($ch);
curl_close($ch);
I edit this post to show an example of what I'm using. I should posted the code in first place.
我编辑这篇文章以展示我正在使用的示例。我应该首先发布代码。
回答by horte
function post($requestJson) {
$postUrl = $this->endpoint."?access_token=".$this->token;
//Get length of post
$postlength = strlen($requestJson);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$postUrl);
curl_setopt($ch,CURLOPT_POST,$postlength);
curl_setopt($ch,CURLOPT_POSTFIELDS,$requestJson);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
//close connection
curl_close($ch);
return $response;
}
回答by hakre
Can anyone help on how can I do to send the data from a form (POST) to an URL using PHP and cURL?
任何人都可以帮助我如何使用 PHP 和 cURL 将数据从表单 (POST) 发送到 URL?
Please search the site and/or post your code how you do it. Looks like that doing the actual POST request is not your concrete problem. In case it is, take a look at this related question: Passing $_POST values with cURLand php curl: i need a simple post request and retrival of page example
请搜索该网站和/或发布您的代码如何操作。看起来做实际的 POST 请求不是你的具体问题。如果是,请查看此相关问题:使用 cURL和php curl传递 $_POST 值:我需要一个简单的发布请求和页面示例检索
Using cURL, all I want to accomplish is send the data, I don't want to be redirected nor get any sort of output (HTML nor TEXT) just submit the data.
使用 cURL,我想要完成的只是发送数据,我不想被重定向或获得任何类型的输出(HTML 或 TEXT),只需提交数据即可。
Assuming $ch
is your curl handle:
假设$ch
是你的卷曲手柄:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
The meaning of the options are described on the PHP manual page.
Then do your curl_exec
and check the return value if the request was successfull or not.
PHP 手册页中描述了这些选项的含义。curl_exec
如果请求成功与否,然后检查返回值。
A probably related question for that part is: Make curl follow redirects?
该部分的一个可能相关的问题是:让 curl 跟随重定向?