Fix your PayPal PHP IPN script

In October 2013  PayPal changed a bit of their server infrastructure. They announced it well, with multiple warnings about requiring the Host: header to be sent with verification requests.

Now suddenly my PHP IPN scripts did not work anymore, data was coming in but nothing got verified.

Checking the response quickly points out that PayPal is now redirecting requests to use HTTPS, but our old scripts are still based on years old example code using fsockopen on port 80. ¬†fsockopen doesn’t know about HTTP redirects.

So, to get your PHP PayPal IPN notifications working again, you should replace the fsockopen line with:

$fp = fsockopen('ssl://www.paypal.com', 443, $errno, $errstr, 30);

Update: A few days later things stopped working again… it seems PayPal now adds a newline character behind the VERIFIED response, messing up the sample script. This can be resolved by replacing:

$res = fgets ($fp, 1024);


with

$res = trim(fgets ($fp, 1024));

3 Responses to “Fix your PayPal PHP IPN script”

  1. Brian Says:

    has the whole process changed now..?

  2. Josiah Says:

    Thanks so much, hopefully this works, and hopefully others find this! I had a mess of orders today with failed IPN, what a pain…

  3. Dan Says:

    Thanks, this worked for me!

Leave a Reply