2

I have the same code running on multiple sites/servers. 2 days ago the code started returning http_code = 0 and the error message "empty reply from server" on one of the servers.

Can anyone shed any light as to why a particular server would be working one day, then not working the next? I have submitted a ticket to the ISP explaining the issue but they cannot seem to find what is wrong (yet).

I guess the question really is, what would/could change on a server to stop this from working?

What is interesting tho is the url I am referencing doesnt get touched on the server returning the error. If I change the url to point to something that doesnt exist, the same error is returned. So it appears that CURL POST references in total are being rejected by the server. I currently have other CURL scripts that are hitting these problem sites that are still working, but they do not have POST options in them.

The issue is definitely related to CURL POST requests on this server, and they are being rejected pretty much immediately.

On the server in question I have 15+ separate accounts and every one of them returns the same result so I dont think its anything I have changed as I know I havent made any wholesale changes to ALL the sites at the time when this issue arose. Of the 6 other sites I have hosted elsewhere, everything is still working fine with exactly the same code.

I have tried various combinations/changes to options from posts I have read but nothing has really made a difference, the working sites still work and the non-working sites still dont.

function sendWSRequest($url, $xml) {

//  $headers[] = 'Content-Type: application/xml; charset=utf-8';
    $headers[] = 'Content-Type: text/xml; charset=utf-8';
    $headers[] = 'Content-Length: ' . strlen($xml);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, true);
//  curl_setopt($ch, CURLINFO_HEADER_OUT, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);

    $result = curl_exec($ch);
    if($result===false) {
        print 'error with curl - '.curl_error($ch).'<br />';
    }

    $info = curl_getinfo($ch);

    curl_close($ch);

    return $result;
}

Any help would be greatly appreciated.

EDIT To summarise based on further investigations, when the script errors, nothing registers in the server access logs. So it appears that CURL requests containing POST options are being rejected before access is granted/logged...

Cheers

Greg J

7
  • Is there anything in PHPs error logs? Commented Sep 12, 2013 at 20:36
  • Redirects will cause this. Try enabling FOLLOWLOCATION. Commented Sep 12, 2013 at 20:37
  • Justin - no, nothing in the logs Commented Sep 12, 2013 at 21:00
  • Amal - followlocation has no impact on the issue whether set to 1 or 0. Just for kicks I logged into an account on a server where the script is working, noticed there were no redirects so add one in to redirect non www to www. the script on this site still works with the new redirect in place. Commented Sep 12, 2013 at 21:03
  • Justin - also just checked the access logs for the server, the requests arent even registering in there! Commented Sep 12, 2013 at 21:14

3 Answers 3

6

I know this is an old thread, but I found a solution that may save someone else a headache:

I just began encountering this exact problem with a web site hosted at GoDaddy which was working until recently. To investigate the problem I created an HTML page with a form containing the same fields being submitted in the POST data via cURL.

The browser-submitted HTML form worked while the cURL POST resulted in the Empty reply from server error. So I examined the difference between the headers submitted by the browser and those submitted by cURL using the PHP apache_request_headers() function on my development system where both the cURL and browser submissions worked.

As soon as I added the "User-Agent" header submitted by my browser to the cURL POST, the problem site worked as expected instead of returning an empty reply:

CURLOPT_HTTPHEADER =>
      array("User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0")

I did not experiment with other/simpler User-Agent headers since this quick fix solved my problem.

Sign up to request clarification or add additional context in comments.

3 Comments

GoDaddy user here. This fixed my problem in Virtual Hosting with an automated cURL request that stopped working on 2014-07-25.
Another Go Daddy user here. This fixed our problem, which started around 2004-07-25, also. (I have no idea why it stopped working in the first place though.)
good solution, but to be more robust for my own code I added the use of $_SERVER['HTTP_USER_AGENT'] and used a preset user-agent string only as a backup in the event that this value was not set.
2

According to the PHP manual, upload should be urlencoded:

CURLOPT_POSTFIELDS The full data to post in a HTTP "POST" operation. [...] This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix. As of PHP 5.5.0, the @ prefix is deprecated and files can be sent using CURLFile.

So you might try with

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'xml=' . urlencode($xml));

and see what happens. Or, anyway, start with an empty or very simple FIELD to see if it at least arrives to the destination server.

Update

I've checked this setup on a test machine and it works. The problem is then likely not to be PHP or cURL side at all, at this point. Can you request a list of software/hardware updates on that machine and network in the last days?

Otherwise, I'd try to capture outgoing traffic so as to determine whether the request leaves the server (and the problem is in between, e.g. a misconfigured firewall: hence my inclusion of "hardware" in the change list), or doesn't leave the server at all. In this latter case the culprits could be:

  • updates to cURL library
  • updates to PHP cURL module and/or PHP binaries
  • updates to "software" firewall rules
  • updates to ancillary network libraries (unlikely; they should be HTTP agnostic and not differentiate a POST from, say, a GET or HEAD)

4 Comments

I had tried this before but just for kicks I did it again, I tried 'test', array('test'), array('test' => '123'), 'xml='.$xml (which is an xml string) and all returned the same 'empty reply' result.
its like its not even getting to look at whats in the options, just drops the request because they exist!
using 'xml=' . urlencode($xml) doesnt work either and actually breaks the script on the server that is working
I've run some tests and updated the answer. I'll be very interested in the outcome of this investigation.
0

OK, as it turns out, a rather reluctant host recompiled Apache2 and PHP which has resolved the issue.

The host claims (their opening statement to my support ticket) that no updates to either Apache2 or PHP had been performed around the time the issue occurred.

the behavior was as such that it wasnt even acknowledging a CURL request that contained the POST commands. The target URL was never reached.

Thank you so much to all who provided their advice. Particularly Isemi who has gone to great lengths to find a resolution.

2 Comments

"No updates have been made". Suuuure. I've lost track of how many times I got this one pulled on me. The code just stopped working of its own accord - with all the symptoms of a botched module update, but that's coincidence! - and mysteriously began working again after recompiling with, we're led to believe, the same configuration. You gotta love these guys :-)
yes well since that posting they did it to me again, for 3 days, and just this month all sites on my reseller plan has been down 3 separate times due to 'core' issues so I am moving yet again to another reseller host. painful exercise. will be my 3rd in last 12 months...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.