Php 返回失败
已解决 感谢@ramraider提供解决方案 解决方案($field\u字符串需要包含原始URL,并且还需要传递到curl\u setop()中) 我有一个简单的HTML网络表单,它可以发布到一个php文件中。在这个php文件中有一个简单的cURL请求。问题是他们(领导管理公司的服务器)不断返回一个“失败”响应。他们非常吝啬地向我提供任何原因,并且不提供任何日志,这样我就可以看到流程失败的地方。他们甚至告诉我我需要使用GET而不是POST,这不仅是他们的文档推荐的POST,而且GET不起作用我已经打印了尽可能多的调试信息,以了解问题所在 如果我复制submission$url并将其与$field_字符串结果结合起来,然后直接在我的浏览器中提交它-它是有效的!因此,我的代码中一定有某种东西会将其丢弃,但在我自己花了整整两天的时间试图研究和解决它之后,我终于请求帮助了 非常感谢您的指导,谢谢 请注意,出于隐私原因,我遗漏了一些URL信息 代码:Php 返回失败,php,curl,Php,Curl,已解决 感谢@ramraider提供解决方案 解决方案($field\u字符串需要包含原始URL,并且还需要传递到curl\u setop()中) 我有一个简单的HTML网络表单,它可以发布到一个php文件中。在这个php文件中有一个简单的cURL请求。问题是他们(领导管理公司的服务器)不断返回一个“失败”响应。他们非常吝啬地向我提供任何原因,并且不提供任何日志,这样我就可以看到流程失败的地方。他们甚至告诉我我需要使用GET而不是POST,这不仅是他们的文档推荐的POST,而且GET不起作用我已
//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);
您的代码尝试使用
$field\u string
来url-ify
发送数据,但是您将$fields
作为数据发送-在我看来,这会产生不同的结果,因为一个发送数据作为多部分/表单数据
,另一个发送数据作为应用程序/x-www-form-urlcoded
,因此主机可能期望不同的内容键入header?另外,我建议将CAINFO
参数添加到curl请求中。非常感谢!您的评论将我疲惫的眼睛指向了正确的方向。我将代码更改为:'//url为POST$field_string=$url.http_build_query($fields,,'&');//打开连接$ch=curlinit($field_string);//设置url、POST变量的数量、POST数据curl_setopt($ch,CURLOPT_POST,1);curl_setopt($ch,CURLOPT_POSTFIELDS,$field_string);“这就成功了!另外,谢谢你关于添加CAINFO的建议-我现在正在研究这个问题。再次感谢你!很高兴你疲惫的眼睛得到了一些缓解
//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);
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