Fixed: GnuTLS recv error (-9): A TLS packet with unexpected length was received 1391/06/30

I was trying to call some RESTFull API functions that were over https using php and curl. There was something wrong with it that was preventing me from communicating with remote server. I was getting this error:

GnuTLS recv error (-9): A TLS packet with unexpected length was received.

The problem is that cURL has not been configured to trust the server’s HTTPS certificate. There are two fixes available  to this issue one that is quick and easy and one that's a bit complicated but more proper.

Easy & Quick Fix

To resolve this issue you can simply tell the curl to skip the server's certificate verifaction by adding these lines to your php code:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);

This fix will remove that error but you should consider the proper fix that has been suggested by Peter Ching if you are concerned about the security of the data being sent and received.


No comments yet! Be the first to send one.

Any thoughts? Please leave a reply

I'll use your email to show your picture using gravatar. I hate spam too.
Back to home

I'm Arash Milani, hacker & happiness ninja.
@narmand is our teams's lab to experiment awesome things in it.

I write and talk about hacking, developing web apps, teamwork and designing for better user experience.

You can always contact me via me[at] email address.