解决
感谢@ramraider提供解决方案。
解决方案($ field_string需要包含原始网址,并且还要传递给curl_setop()
//url-ify the data for the POST
$field_string = $url . http_build_query($fields, '', '&');
//open connection
$ch = curl_init($field_string);
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $field_string);
我有一个简单的HTML网页表单,可以发送到php文件。在那个PHP文件是一个简单的cURL请求。问题是他们(领导管理公司的服务器)不断返回“失败”响应。他们在为我提供任何原因以及为什么不提供任何日志的原因时都非常吝啬,因此我可以看到流程失败的地方。他们甚至告诉我,我需要使用GET而不是POST,这不仅使他们的文档推荐 POST,而且GET也不起作用。我已尽可能多地打印出调试信息,以便了解问题所在。
如果我复制提交$ url并将其与$ field_string结果组合并直接在我的浏览器中提交 - 它可以工作!所以,它必须是我的代码中的东西,它正在抛弃它,但经过整整两天的尝试研究和自己解决它,我终于寻求帮助。
非常感谢任何指导。谢谢你。
请注意,出于隐私原因,我遗漏了一些网址信息。
CODE:
//extract data from the post
//set POST variables
$url = 'https://website.com/Import.aspx?';
$fields = array(
'Provider'=> 'SabbaraWebsiteGET',
'Client'=>'SabbaraGroup',
'CampaignId'=>'1064',
'first_name' => urlencode($_POST['first_name']),
'last_name' => urlencode($_POST['last_name']),
'user_email' => urlencode($_POST['email']),
'phone' => urlencode($_POST['phone']),
'current_payment' => urlencode($_POST['current_payment']),
'type_loan' => urlencode($_POST['type_loan']),
'reason_application' => urlencode($_POST['reason_application']),
);
//url-ify the data for the POST
$field_string = http_build_query($fields, '', '&');
//open connection
$ch = curl_init($url);
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
//execute post
$response = curl_exec($ch);
// DEBUGGING
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo '<pre>
Request Header:
'.curl_getinfo($ch,CURLINFO_HEADER_OUT ).'
'.$field_string.'
Response:
'.$response.'
</pre>';
$info = curl_getinfo($ch);
echo "\n\n" . 'Took '. $info['total_time']. ' seconds to send a request to '. $info['url']. ' CODE: '.$info['http_code']."\n";
//close connection
curl_close($ch);
来自cURL的调查结果
Request Header:
POST /Import.aspx HTTP/1.1
Host: website.com
Accept: */*
Content-Length: 1147
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------e7ceb9b6f9be238e
Provider=SabbaraWebsiteGET&Client=SabbaraGroup&CampaignId=1064&first_name=Test&last_name=Submission&user_email=test%2540test.com&phone=1234568547¤t_payment=151-300&type_loan=Federal%2BAnd%2BPrivate&reason_application=Credit%2BRepair
Response:
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/7.5
Set-Cookie: ASP.NET_SessionId=23zhmtzihv34bxc1abj2rbhu; path=/; HttpOnly
X-Powered-By: ASP.NET
Date: Tue, 23 May 2017 21:31:29 GMT
Content-Length: 8
Failure
Took 0.253105 seconds to send a request to https://website.com/Import.aspx? CODE: 200