任何人都可以帮助我如何使用PHP和cURL将表单(POST)中的数据发送到URL?
使用cURL,我想要完成的只是发送数据,我不想被重定向,也不想获得任何类型的输出(HTML或TEXT)只提交数据.
很高兴知道数据是否已成功提交以处理错误或重定向.
附加信息.我认为我被重定向的原因是因为一旦我执行了cURL,目标页面(第三方网站)就会重定向到确认用户已经收到他们的数据,并且由于某种原因,当我使用cURL发送我的数据,他们的重定向会影响我的页面,因此我的页面也会重定向到他们的确认网站.
谢谢你们.
示例代码:
$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=me@gmail.com&work_phone=123-456-7890");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
$resp = curl_exec($ch);
curl_close($ch);
Run Code Online (Sandbox Code Playgroud)
我编辑这篇文章来展示我正在使用的一个例子.我应该首先发布代码.
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;
}
Run Code Online (Sandbox Code Playgroud)