Back 14 Comments

HTTP 1.1 PayPal IPN Example PHP Code

PayPal recently announced that they're going to "discontinue support for HTTP 1.0 protocol starting February 1, 2013."  Today I ran into some problems implementing these changes.  It turns out that the VERIFIED response code is now followed by a \r\n.  So, the previous if (strcmp($res, "VERIFIED") == 0) { code no longer works by itself.  I was able to solve this by trimming off the \r\n using  $res = trim($res);.  I've posted some example code here in case anyone else is having trouble making their old IPN scripts work with PayPal's new requirements. 

View PHP code


//read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';

foreach ($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";

//post back to PayPal system to validate (replaces old headers)
$header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Host:\r\n";
$header .= "Connection: close\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('ssl://', 443, $errno, $errstr, 30);

//error connecting to paypal
if (!$fp) {
//successful connection    
if ($fp) {
    fputs ($fp, $header . $req);
    while (!feof($fp)) {
        $res = fgets ($fp, 1024);
        $res = trim($res); //NEW & IMPORTANT
        if (strcmp($res, "VERIFIED") == 0) {
            //insert order into database    
        if (strcmp ($res, "INVALID") == 0) {
            //insert into DB in a table for bad payments for you to process later



Ivor Humphreys
September 29, 2012 4:55pm
Many thanks for this. Using trim() as you describe solved the problem for me too.
October 2, 2012 6:47am
Your a star! Thanks for sharing this i just spent 2 days trying to work out why my script stopped working.
All the best,
October 10, 2012 7:50am
Thanks it's probably THE solution !
November 15, 2012 9:42am
Trim! Thanks! I've been pulling my hair out :/ How frustrating that PayPal does something so dumb that affects so many sites!
Thomas Eder
November 30, 2012 9:32pm
THANK YOU! You saved a life tonight, I was minutes away from throwing myself off the roof!
Lala King
December 25, 2012 2:00am
Good day and welcome to check here.
December 26, 2012 3:45pm
why am I geting 500 Internal Server Error and header connection: close ? any ide?
December 31, 2012 1:13pm
Many, many thanks for publishing this, I have spent two days trying to sort it out.
January 6, 2013 9:13am
Thanks !! Couldn't figure out why i didn't get any response :) workds for me now
January 8, 2013 2:23pm
Thanks for this, got my cart updated and working with ease using this, saved me a lot of time!
January 23, 2013 10:14am
Thank you for that, been trying to get this code to work for days, every time I ran it in the Sandbox it hung.

Found out I needed your highlighted amendment, but I also needed the following line from your code, which was not in the PayPal suppied example.

$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

January 28, 2013 12:21pm
Today Paypal IPN doesn't seem to work. Anyone else having issues today?
It was working fine yesterday.
Me too
January 28, 2013 7:03pm
Yes, I am getting INVALID in my:
while (!feof($fp)) {
$res = fgets ($fp, 1024);
February 8, 2013 7:23am

I have done this, and when I try to pay with credit card ...
It's not working ...
It's reloading the payment page !
Any ideas?


Your comment has been posted....