简体   繁体   English

服务器不响应通过PHP和CURL发送的POST数据

[英]Server not responding to POST data sent via PHP and CURL

I'm trying to replicate a form submission that pulls back some analytics data using CURL in PHP. 我正在尝试复制一个表单提交,该表单提交使用PHP中的CURL提取一些分析数据。 I have copied the raw POST from fiddler and used it to inform my PHP script. 我已经从fiddler复制了原始POST,并用它来通知我的PHP脚本。

This is my CURL function 这是我的CURL函数

function executeCurl($data, $certificate=false) {
    $ch = curl_init();
    //set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, $data['header']);
    curl_setopt($ch, CURLOPT_URL, $data['url']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data['postdata']);
    curl_setopt($ch, CURLOPT_POST, $data['post']);
    curl_setopt($ch, CURLOPT_COOKIE, $data['cookie']);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $data['httpheader']);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    if($certificate) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "\DigiCertHighAssuranceEVRootCA.crt"); 
    }

    //execute post
    $result = curl_exec($ch);

I receive a response from the server but the post data has been ignored. 我收到了服务器的响应,但帖子数据已被忽略。 This is my POST request. 这是我的POST请求。

POST /report.aspx?tid=3477 HTTP/1.1
Host: blah.com
Cookie: somecookies;
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: https://blah.com/report.aspx?tid=3477
Accept-Language: en-GB,en;q=0.8,en-US;q=0.6
Content-Length: 7418
Expect: 100-continue

The $data['postdata'] value is an exact copy of the url encoded data string that I copied from Fiddler. $ data ['postdata']值是我从Fiddler复制的url编码数据字符串的精确副本。 The rest of the headers are identical except the Expect: 100-continue header, which is added by CURL. 除由CURL添加的Expect:100-continue头以外,其余的头均相同。

You can see the content type is set appropriately. 您可以看到内容类型设置正确。

Additionally, HTTPS is being used and it makes no difference whether I use the certificate or not. 此外,正在使用HTTPS,无论是否使用证书都没有区别。

So, I get a response from the server but the post fields are ignored. 因此,我从服务器收到响应,但发布字段被忽略。 Does anyone have any suggestions as to why this might be? 有人对这可能会有什么建议吗?

Thanks in advance. 提前致谢。

EDIT adding "Expect:" to my HTTPHEADER means the POST now looks as follows, but the server still does not respond to the posted data. 编辑到我的HTTPHEADER中添加“期望:”意味着POST现在看起来如下,但是服务器仍然不响应发布的数据。

POST /report.aspx?tid=3477 HTTP/1.1
Cookie: blah
Host: blah.com
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: https://blah.com
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: https://blah.com/report.aspx?tid=3477 HTTP/1.1
Accept-Language: en-GB,en;q=0.8,en-US;q=0.6
Content-Length: 7418

As far as I understand, cURL won't send the payload unless the server responds with 100 Continue after receiving that header. 据我了解,除非服务器在收到该标头后以100 Continue响应,否则cURL不会发送有效负载。 Try curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); 尝试curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); to disable this expectation. 禁用此期望。

I solved this by visiting the page twice. 我通过两次访问页面解决了此问题。 Once to retrieve the __VIEWSTATE, PagePostbackID, and PublicKeyToken variables. 一次检索__VIEWSTATE,PagePostbackID和PublicKeyToken变量。

The second time I used these variables in my request and the POST worked. 第二次我在请求中使用这些变量,并且POST起作用了。

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

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